我必须在同一工作簿中搜索sheet1中的列标题,如果sheet1的A列和sheet2的C列匹配,则将其中的单元格复制到sheet2的AZ列。
下面的Macro复制地址非常好。我必须在其中复制值。但是,如果我改变到值在行结束时,下面的宏,它不工作。你的帮助将是感激。
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
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 range
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 did not work. Now, I specified sheet, and it works
set mYvalue = w1.Cells(oCell.Row, colname)
Dic.Add oCell.Value, mYvalue.value
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(, 52).Value = Dic(key)
End If
Next
Next
End Sub