如果 A 和 D 匹配,将 A 粘贴到 G 中。然后添加 B 和 E 并将结果放在 H 列中。我在将 B 和 E 中的值添加到 H 时遇到问题。
Private Sub CommandButton1_Click()
Dim rng1 As Range
Dim rng2 As Range
Dim RowNo As Long
Dim LR As Long
Set rng1 = Worksheets("Sheet1").Range("D1:D100", Worksheets("Sheet1").Range("D" & Rows.Count).End(xlUp))
Set rng2 = Worksheets("Sheet1").Range("A1:A100", Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp))
For Each d In rng1
LR = Range("H" & Rows.Count).End(xlUp).Row
Range("F1:F" & LR).Formula = "=H1+C1"
If Not d.Value = "" And Application.WorksheetFunction.CountIf(rng2, d) > 0 Then
RowNo = Application.WorksheetFunction.Match(d, rng2)
If d.Offset(, 1).Value = "" Then d.Offset(, 3).Resize(1).Value _
= Worksheets("Sheet1").Range("A" & RowNo).Value
If d.Offset(, 1).Value = "" Then d.Offset(, 4).Resize(1).Value _
= Worksheets("Sheet1").Range("B" & RowNo).Value
End If
Next d
End Sub
示例单元格:
A 1 X 22 A 45
B 2 C 33 C 36
C 3 A 44 F 105
D 4 Y 55
E 5 J 66
F 6 O 77
G 7 T 88
F 99
W 11