|
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i&, r1&, r2&, d, arr
With Sheets("Sheet1")
r1 = .Cells(.Rows.Count, 1).End(3).Row '取第一列中数据最大行号
If Target.Column <> 1 Then Exit Sub '不是第一列,退出程序
If Target.Row < 2 Or Target.Row > r1 Then Exit Sub '不是第2行到有数据的最大行,退出程序
End With
Set d = CreateObject("Scripting.Dictionary")
With Sheets("Sheet2")
r2 = .Cells(.Rows.Count, 8).End(3).Row '取第8列中数据最大行号
arr = .Range("G1:H" & r2).Value
For i = 2 To UBound(arr)
If arr(i, 2) <> "" Then d(arr(i, 2)) = arr(i, 1)
Next
End With
With Sheets("Sheet1")
If d.Exists(Target.Value) Then Target.Offset(0, 1).Value = d(Target.Value)
End With
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i&, r1&, r2&, d, arr
With Sheets("Sheet1")
r1 = .Cells(.Rows.Count, 5).End(3).Row '取第一列中数据最大行号
If Target.Column <> 5 Then Exit Sub '不是第一列,退出程序
If Target.Row < 2 Or Target.Row > r1 Then Exit Sub '不是第2行到有数据的最大行,退出程序
End With
Set d = CreateObject("Scripting.Dictionary")
With Sheets("Sheet2")
r2 = .Cells(.Rows.Count, 8).End(3).Row '取第8列中数据最大行号
arr = .Range("G1:H" & r2).Value
For i = 2 To UBound(arr)
If arr(i, 2) <> "" Then d(arr(i, 2)) = arr(i, 1)
Next
End With
With Sheets("Sheet1")
If d.Exists(Target.Value) Then Target.Offset(0, 1).Value = d(Target.Value)
End With
End Sub
大侠请帮助我如何把这两段进行合并,谢谢大侠! |
|