【发布时间】:2018-01-25 09:58:39
【问题描述】:
我发现这段代码可以满足我 99% 的需求。
过程描述:在我的工作簿中,有一个带有命名列的 SQL 工作表,基于列标题我必须遍历工作簿中列标题具有相同名称的所有其他工作表(从 10 到 50 个工作表) ,源 SQL 表中的所有列都将复制到目标表中。在目标表中,列标题由 4 行组成,在源中,列标题只有 1 行。
问题 1:如何复制没有标题的列并粘贴偏移量为 4 行的内容。
问题 2:如何仅复制实际使用的范围,工作簿越来越大。
代码示例:
Sub Test()
Dim Sh2Cell As Range
Dim Sh3Cell As Range
Dim ShQuelleTitle As Range
Dim ShZielTitle As Range
'Here we loop through the Range where the Title Columns for source and goal sheet are stored
'The columns in the Source Sheet do not have the same order as in the Goal Sheet
Set ShQuelleTitle = Sheets("SQL").Range("SQL_Titel")
Set ShZielTitle = Sheets("Ziel").Range("Ziel_Titel")
For Each Sh2Cell In ShQuelleTitle
For Each Sh3Cell In ShZielTitle
If Sh2Cell = Sh3Cell Then
Sh2Cell.EntireColumn.Copy Sh3Cell.EntireColumn
' Problem-1 is: in the goal sheet the copy range has to be shifted 4 rows down because
' i have different column title structure which has to be maintained (with this goal
' sheet there happens a txt-export from another external developer.
' Problem-2 is: how can i only copy and paste cells with content - the worksheets are getting
' huge on file size if the copy range has some weird formatting
End If
Next
Next
End Sub
【问题讨论】:
-
你应该展示你试图解决你的 2 个问题的代码。如果您展示您已经为此付出的努力,其他人可以提供帮助。至少对于第二个问题,您会发现许多教程,例如。如何找到最后使用的单元格。要复制没有标题,请找到最后使用的行(许多教程可用于此)并从第 2 行复制到该行。付出一些努力,展示你的尝试,并告诉你哪里出错或卡住了。