[英]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
I want sheets "Status" and "Ref" compared. 我想将“状态”表和“参考”表进行比较。 If the values match in both columns Status!F and Ref!B AND Status!Q and Ref!C for a particular individual's data in a row, then copy the value from Ref!F to Status!H 如果某行中某个人的数据的“状态” F和“参考” B 和“状态” Q和“参考” C列中的值均匹配,则将值从“参考” F复制到“状态”H。
This is my first ever attempt at writing code so it's probably full of errors, but the debugger points out the first If statement in particular. 这是我第一次尝试编写代码,因此可能充满了错误,但是调试器特别指出了第一个If语句。 I've tried it with and without parentheses. 我试过了,没有括号。
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
edit - a note--these sheets are not in the same order. 编辑-注释-这些工作表的顺序不同。 so row 1500 in Status could match something on row 3 on ref, for example 因此,例如,状态中的第1500行可以与参考第3行上的内容匹配
Try this 尝试这个
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.