Hello all,
I could use some help.
I have two worksheets "W1" and "W2", each with multiple rows and columns, whose numbers may be subject to change. I need to go row by row in "W2", starting from row 2 and compare the values in columns A and C in "W2" to the columns A and C of every row in "W1", starting from row 2 respectively. If the value in "W2"'s column A is unique, then I need to copy the entire row to "W1". If a value of column "A" in one of the rows of "W2" is already existing in "W1", I need to check if the corresponding column C also matches. If the value in C is unique, then I need to copy this row to "W1" as well.
For example:
W1:
W2:
So in the above case, the rows starting with S3, S5 and S4 from "W2" should be added to "W1", and the row starting with "S1" will be ignored, since it already exists. The comparison has to be done for Columns A and C.
I have the following code so far
I think I understand that my "And" statement won't do the work, but I am not sure how to go ahead with this. I'm very new to Excel VBA and cannot seem to find a way to resolve this.
Hope you can help me.
Thank you so much.
I could use some help.
I have two worksheets "W1" and "W2", each with multiple rows and columns, whose numbers may be subject to change. I need to go row by row in "W2", starting from row 2 and compare the values in columns A and C in "W2" to the columns A and C of every row in "W1", starting from row 2 respectively. If the value in "W2"'s column A is unique, then I need to copy the entire row to "W1". If a value of column "A" in one of the rows of "W2" is already existing in "W1", I need to check if the corresponding column C also matches. If the value in C is unique, then I need to copy this row to "W1" as well.
For example:
W1:
Sl. no. | H1 | H2 |
S1 | abcd | 2 |
S4 | xyz | 503 |
S2 | asd | 33 |
W2:
Sl. no. | H1 | H2 |
S3 | pqr | 10 |
S5 | asdfg | 503 |
S1 | abcd | 2 |
S4 | xyz1 | 1000 |
So in the above case, the rows starting with S3, S5 and S4 from "W2" should be added to "W1", and the row starting with "S1" will be ignored, since it already exists. The comparison has to be done for Columns A and C.
I have the following code so far
VBA Code:
Sub RowTransfer()
Application.ScreenUpdating = False
Dim lastRow As Long
lastRow = Sheets("W2").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim rnge As Range
Dim valCheck As Range
Dim valCheckC As Range
For Each rnge in Sheets("W2").Range("A2:C" & lastRow)
Set valCheck = Sheets("W1").Range("A:A").Find(rnge, lookIn:=xlValues, lookat:=xlWhole)
Set valCheckC = Sheetws("W1").Range("B:B").Find(rnge, lookIn:xlValues, lookat:=xlWhole)
If valCheck Is Nothing And valCheckC Is Nothing Then
rnge.EntireRow.Copy
Sheets("W1").Cells(Rows.Count, "A").End(xlUp).Offset(1,0).PasteSpecial xlPasteValues
End If
Next rnge
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
I think I understand that my "And" statement won't do the work, but I am not sure how to go ahead with this. I'm very new to Excel VBA and cannot seem to find a way to resolve this.
Hope you can help me.
Thank you so much.