PDA

View Full Version : VLOOKUP



_Br_10
02-22-2023, 09:06 AM
Hi everyone,
I want to use de function "vlookup" on vba, but when the macro cannot search the value X on page ("CNM_ABER") i pretend that the variable y will be EMPTY. What you recommended that i do?
In this moment i have this code:



For i = 1 To 169
Worksheets("CNM_LIB").Select
x = Cells(1 + i, "F").Value
Worksheets("CNM_ABER").Select
On Error Resume Next
y = WorksheetFunction.VLookup(x, Range("F1:N221"), 8, False)
'y = WorksheetFunction.IfError(WorksheetFunction.VLookup(x, Range("F1:N221"), 8, False), "")
On Error GoTo 0
End Sub

June7
02-22-2023, 12:15 PM
Why is there not a Next line to accompany the For? On Error Resume Next has nothing to do with controlling a loop iteration.

Do not need to Select worksheets or other objects in order to act on them.

Cells property requires numeric arguments

x = Worksheets("CNM_LIB").Cells(1 + i, 6).Value

p45cal
02-22-2023, 03:06 PM
Cells property requires numeric arguments
Cells(22,"F") is good.

Guessing:
For i = 1 To 169
y = Application.VLookup(Worksheets("CNM_LIB").Cells(1 + i, "F").Value, Worksheets("CNM_ABER").Range("F1:N221"), 8, False)
If IsError(y) Then 'it's not found
'do something (or nothing?)
Else 'it has been found (and y contains the result)
'do something else
End If
Next i

June7
02-22-2023, 03:48 PM
Well, dagnabbit! Would be nice if MS docs mentioned that.

Could have sworn I tried it at some time long ago.