【发布时间】:2020-08-31 03:23:19
【问题描述】:
如果 sheet1 的 A 列和 Sheet2 的 C 列匹配,我必须在同一工作簿中的 sheet1 中搜索列标题并将单元格从其中复制到 Sheet2 列 AZ。
下面的宏复制地址很好。我必须在其中复制价值。但是,如果我在宏下面指示的行的末尾更改为 value,它就不起作用。您的帮助将不胜感激。
Sub SearchCopy()
Dim Dic As Object, key As Variant, oCell As Range, i&
Dim w1 As Worksheet, w2 As Worksheet
Set Dic = CreateObject("Scripting.Dictionary")
Set w1 = Workbooks("CSheet.xlsx").Sheets("stock")
Set w2 = Workbooks("alcSheet.xlsx").Sheets("Sale")
i = w1.Cells.SpecialCells(xlCellTypeLastCell).Row
Dim aCell As Range
Dim colname As String
Dim col As Long
'Dim mYvalue As Long
Dim myNum As String
With w1
Set aCell = .Range("A1:AZ1").Find(What:="customer", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
col = aCell.Column
colname = Split(.Cells(, col).address, "$")(1)
End If
For Each oCell In w1.Range("B2:B" & i)
If Not Dic.Exists(oCell.Value) Then
' If I change to value at the end of the line below, it does not work.
mYvalue = Cells(oCell.Row, colname).address
Dic.Add oCell.Value, mYvalue
End If
Next
End With
i = w2.Cells.SpecialCells(xlCellTypeLastCell).Row
For Each oCell In w2.Range("A2:A" & i)
For Each key In Dic
If oCell.Value = key Then
oCell.Offset(, 39).Value = Dic(key)
End If
Next
Next
End Sub
【问题讨论】:
-
有什么理由不使用 HLOOKUP / VLOOKUP fx?
-
我必须在不同的地方使用多张具有相同列标题的工作表。我别无选择。我试过这个方法。我只能复制地址,不能复制值。现在,我发现了发生了什么。我找到了我错过的东西。感谢您的回复。
标签: search google-sheets copy cell columnheader