PDA

View Full Version : VBA - function Vlookup



_Br_10
11-02-2022, 02:37 AM
Hi everyone,
In my module i pretend search a value corresponding to a variable a.
But when i run the code variable h are incorrect, because apeear error 2042 and the suppose is appear a number.

My code is:


Sub Macro3()
Dim j As Variant
Dim a As Variant
Dim h As Variant 'variavel de teste
Set Model = Workbooks("AtrasosTotal.xlsm").Worksheets("Model")
lastRowZModel = Model.Cells(Model.Rows.Count, "A").End(xlUp).Row
a = 1600104092
Sheets("Model").Select
Set b = Range("A2:O" & lastRowZModel)
h = Application.VLookup(a, b, 12, False)
End Sub



How can I resolve this problem

snb
11-02-2022, 02:45 AM
Wrap VBA code in Code tags !


Sub M_snb()
sn=Workbooks("AtrasosTotal.xlsm").sheets("Model").usedrange

for j=1 to ubound(sn)
if sn(j,1)=1600104092 then exit for
next

msgbox sn(j,12)
End sub

arnelgp
11-02-2022, 05:13 AM
google error 2042.
What is the reason for VBA error 2042? - Quora (https://www.quora.com/What-is-the-reason-for-VBA-error-2042)

add an error handler to your code.

_Br_10
11-02-2022, 07:11 AM
Nice! It's working, but isn't efficient, because i have many lines for execute the cicle FOR.
You have any sugestion?