most
Board Regular
- Joined
- Feb 22, 2011
- Messages
- 107
- Office Version
- 365
- 2019
- Platform
- Windows
- Mobile
Can anyone assist me, fixing this macro? I want it to lookup each name in Sheet1, find it in Sheet2 and return the ID number to Sheet1. The challenge is that the name lookup shouldn't be exact, i.e. "lisa flisa" equals "lisa", so "lisa flisa" would get the ID 20.
*Sheet1*
Column C Column F
Name ID
kalle balle
lisa flisa
arne
lisa flisa
*Sheet2*
Column B Column E
Name ID
arne 10
lisa 20
kalle 30
I'm using Excel 2010.
*Sheet1*
Column C Column F
Name ID
kalle balle
lisa flisa
arne
lisa flisa
*Sheet2*
Column B Column E
Name ID
arne 10
lisa 20
kalle 30
Code:
Sub IterateNames()
For i = 2 To 65535 Step 1
c = Sheets("Sheet1").Range("C" & CStr(i)).Value
If IsEmpty(c) Then Exit For
Sheets("Sheet1").Range("F" & CStr(i)).Value = LookupName(c)
Next i
End Sub
Private Function LookupName(ByVal name As String) As Variant
LookupName = ""
For i = 2 To 65535 Step 1
b = Sheets("Sheet2").Range("B" & CStr(i)).Value
If IsEmpty(b) Then Exit For
If InStr(1, LCase(CStr(b)), LCase(CStr(name)), vbTextCompare) > 0 Then 'Here is the problem, I think
LookupName = Sheets("Sheet2").Range("E" & CStr(i)).Value
Exit For
End If
Next i
End Function
I'm using Excel 2010.