![](/img/trans.png)
[英]Comparing two columns in one Excel sheet, to two columns in another sheet, and if they match, copy data from another column
[英]Excel VBA - IF/AND Issue - If values from two columns match on separate sheets, copy a value from one sheet to another in another column
我想将“状态”表和“参考”表进行比较。 如果某行中某个人的数据的“状态” F和“参考” B 和“状态” Q和“参考” C列中的值均匹配,则将值从“参考” F复制到“状态”H。
这是我第一次尝试编写代码,因此可能充满了错误,但是调试器特别指出了第一个If语句。 我试过了,没有括号。
Sub help()
Dim i As Long
Dim j As Long
Dim index As Integer
Sheet1LastRow = Worksheets("Status").Range("F" & Rows.Count).End(xlUp).Row
Sheet2LastRow = Worksheets("Ref").Range("B" & Rows.Count).End(xlUp).Row
For index = 1 To Sheet1LastRow
If ((Worksheets("Status").Cells(i, 6).Value = Worksheets("Ref").Cells(j, 2).Value) And (Worksheets("Status").Cells(i, 17).Value = Worksheets("Ref").Cells(j, 3).Value)) Then Worksheets("Status").Cells(i, 8).Value = Worksheets("Ref").Cells(j, 6)
Next
index = index + 1
j = j + 1
i = i + 1
Do Until index = Sheet1LastRow
Loop
End Sub
编辑-注释-这些工作表的顺序不同。 因此,例如,状态中的第1500行可以与参考第3行上的内容匹配
尝试这个
For i = 1 To Sheet1LastRow
For j = 1 To Sheet2LastRow
If ((Worksheets("Status").Cells(i, 6).Value = Worksheets("Ref").Cells(j, 2).Value) and (Worksheets("Status").Cells(i, 17).Value = Worksheets("Ref").Cells(j, 3).Value)) Then
Worksheets("Status").Cells(i, 8).Value = Worksheets("Ref").Cells(j, 6).Value
Exit for
End if
Next j
Next i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.