I want to insert corresponding Column C
values in Column D
when Column A and Column B value matches.
For example:
Column A2 is equal to Column B2, now Column C2 value is posted on Column D2
OR
Column A7 is equal to Column B3 then Column C3 value is posted on Column D3
For detail please see the screen shot so you have idea what i am trying to do.
[Please click to see the screenshot][1]
The code which i am trying is below but it is not working properly, it is just giving only one cell value:
Private Sub ForComparing_Click()
Dim ws As Worksheet
Dim cel As Range
Dim lastRowA As Long, lastRowB As Long, lastRowC As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
lastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row 'last row of column A
lastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row 'last row of column B
lastRowC = .Cells(.Rows.Count, "C").End(xlUp).Row 'last row of column C
For Each cel In .Range("A2:A" & lastRowA) 'loop through column A
'check if cell in column A exists in column B
If WorksheetFunction.CountIf(.Range("B2:B" & lastRowB), cel) = 0 Then
.Range("D" & cel.Row) = "No Match"
Else
.Range("D" & cel.Row) = .Range("C" & cel.Row)
End If
Next
End With
End Sub
Edited 1:
Please see the output of this code below: Click here to see screen shot
Column A3
should compare with Column B5
because the value D
is equal in this case, and then it should print Column C5
value to Column D3
Furthermore, it should give value in Column D
for every value of Column A
but it stop after first 4 values.
Thanks for your time.
Edited 2:
What you have just edited is perfectly right, but i want to do this for each Column A
value.
I want to compare each Column A
value with Column B
and then corresponding Column C
value is copied on Column D
.