I get 1004 error when trying to do a Vlookup/Index-Match, or no results at all.
I tried both vlookup and Index /match inside a While loop.
I have used basically the same loop in many of my macros without any issues so far. I tried adjusting the formats (even with Format painter).
I isolated the values for the lookup in a new file and did just the lookup part.
Note: A manual Vlookup or index.Match works.
Code:
Sub lookup()
'Variables for the Vlookups
Dim newRow As Long
Dim newCL As String
newRow = 7
Dim rn As Range
Set rn = Worksheets("trysheet").Range("C:C")
Dim mrange As Range
Set mrange = Worksheets("trysheet").Range("A:A")
On Error Resume Next
'lookups
Do While Worksheets("test").Cells(newRow, 4).Value <> ""
newCL = Worksheets("test").Cells(newRow, 4).Value
Worksheets("test").Cells(newRow, 5) = Application.Index(rn,
Application.Match(newCL, mrange, 0))
newRow = newRow + 1
Loop
End Sub
I have tried using Application.WorksheetFunction instead of just Application. but then I either receive
1004 error - "Unable to get Match/Vlookup property of the WorksheetFunction class"
or
Application defined error
EDIT:
I achieved the required result using the traditional vlookup function (with the help of macro recording and modifying my loop):
Sub lookup()
'Variables for the Vlookups
Dim newRow As Long
newRow = 7
Dim rn As Range
Set rn = Worksheets("trysheet").Range("C:C")
Dim mrange As Range
Set mrange = Worksheets("trysheet").Range("A:A")
On Error Resume Next
'
'Get Employee
Do While Worksheets("test").Cells(newRow, 4).Value <> ""
Worksheets("test").Cells(newRow, 5).FormulaR1C1 = "=VLOOKUP(RC[-1], trysheet!C[-4]:C[-2],3,0)"
newRow = newRow + 1
Loop
End Sub
I usually try to avoid this method and stick to the application.WorksheetFunction or application.Vlookup(example) because I find it more clean and automatically removes the formula, but in this case does the job.
Still help me understand why the application method did not work here, because this happens to me for the first time.
yytsunamiyy had some good suggestions below, but still not complete.