我想将数据从工作表1的特定列复制到工作表2的特定列。有20个这样的列,并且在
之类的表中维护了映射我已经编写了用于从中搜索列名(源表和目标表)的代码,但是无法将数据从源列(动态范围)复制到目标列。
Sub search_validate()
Dim j As Integer
Dim sourcSearch, destSearch As String
Dim sCell, dCell As Range
For j = 3 To 20
sourcSearch = Sheet6.Range("Z" & j).Value ' pickup selected source column name
destSearch = Sheet6.Range("AA" & j).Value ' pickup selected destination column name
Set sCell = Sheet1.Rows(2).Find(What:=sourcSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Set dCell = Sheet2.Rows(2).Find(What:=destSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
' sCell.Address or sCell.Column to get source column header address but data starts after this column. dynamic range
' dCell.Address or dCell.Column to get destination column header address. no data in destination column except header.
Next j
End Sub
答案 0 :(得分:1)
这应该将数据附加到目标列的末尾
If Not sCell Is Nothing And Not dCell Is Nothing Then
Dim Source As Range, Target As Range
Set Source = Intersect(Sheet1.UsedRange, sCell.EntireColumn).Offset(1)
Set Target = Sheet2.Cells(Sheet2.Rows.Count, dCell.Column).End(xlUp).Offset(1)
Source.Copy Destination:=Target
End If
答案 1 :(得分:0)
您需要在当前表的最后一行之后粘贴新数据吗? 您可以用以下内容找到最后一行:
lastRow = Columns("Enter your column number here").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
然后像这样复制粘贴:
Set copyRange = Range(YOUR RANGE HERE)
copyRange.Copy
Cells(lastRow + 1, "Enter your column number here").PasteSpecial Paste:=xlPasteValues