I am trying to do a partial comparison between 2 columns from 2 different sheets in the same workbook.
For example: Sheet2's Column B
contains the Rs ID
(all numbers) and Column A
contains Clinical Significance
, in Sheet1 there are 2 columns A
& B
(contains strings and numbers) as well with the same headers.
If there is a partial match in Column B
of Sheet2 with Column B
of Sheet1, I will want my VBA code to copy the cell in Column A
from Sheet2 to the same cell in in Column A
in Sheet1.
Sheet1:
Sheet2:
This is my code. It runs perfectly but it doesnt seem to capture any data as the Column B
in sheet2 is not exactly the same as Column A
. Could it be I used the lookat:=xlPart
incorrectly?
Sub test()
Dim rng2 As Range, c2 As Range, cfind As Range
Dim x, y
With Worksheets("sheet1")
Set rng2 = .Range(.Range("B2"), .Range("B2").End(xlDown))
For Each c2 In rng2
x = c2.Value
With Worksheets("sheet2").Columns("B:B")
On Error Resume Next
Set cfind = .Cells.Find(what:=x, lookat:=xlPart, LookIn:=xlValues)
If (Not (cfind Is Nothing)) Then
y = cfind.Offset(0, -1).Value
c2.Offset(0, -1) = y
End If
End With
Next c2
End With
End Sub