我正在寻找以下情况的VBA代码:
excel工作表中有四列(A,B,C,D),并且代码应填充D列。
逻辑应为
这些列是包含400个条目的长列表。
答案 0 :(得分:0)
尝试一下:
Sub MySub()
Dim lastRow As Long, i As Long
lastRow = Cells(Rows.Count, 3).End(xlUp).Row
For i = 2 To lastRow
If Cells(i, 1).Value = Cells(i, 3).Value Then
Cells(i, 4).Value = Cells(i, 2).Value
Else If Cells(i + 1, 1).Value = Cells(i, 3).Value Then
Cells(i, 4).Value = Cells(i + 1, 2).Value
End If
Next
End Sub
答案 1 :(得分:0)
Sub test()
Dim x As Integer
Dim erow As Integer
'erow takes in the value of the last row number.
erow = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For x = 1 To erow
For i = x To erow
If Cells(x, 3).Value = Cells(i, 1).Value Then
Cells(i, 2).Select
Application.CutCopyMode = False
Selection.Copy
Cells(x, 4).Select
ActiveSheet.Paste
Application.CutCopyMode = False
i = erow
End If
Next i
Next x
End Sub
这就是您所需要的。它不仅为第二个D列对每个D列执行此操作。