VBA Error when displaying data from a Worksheet

Asked

Viewed 200 times

0

I’m experimenting VBA and managed to add data to a BD in sheet 2.

And now I tried to do to show the data on sheet 1 where I choose the ID and it shows the data entered there, but it’s giving me a mistake.

Sub Busca()
Dim valor As Integer

 valor = InputBox("Numero:", "buscar")

 Range("C24") = valor

 Range("C28") = Application.WorksheetFunction.VLookup(valor, Sheets(2).Range("A:B"), 2, False)

 End Sub

The mistake I have is

Run time error 1004
It is not possible to obtain the Vlookup property of the Worksheetfunction class

This function has worked before, but now gives me error and I do not understand why.

1 answer

0

The Vlookup function is not finding value, so the error occurs. There are some ways to deal with the error, because if you use the function in the spreadsheet the #N/A error will appear.

Sub Busca()
Dim valor As Long

 valor = InputBox("Numero:", "buscar")

 Sheets(1).Range("C24") = valor

 NumCel = Application.VLookup(valor, Sheets(2).Range("A:B"), 2, False)

If IsError (NumCel) Then
    Sheets(1).Range("C28") = xlErrNA
Else
    Sheets(1).Range("C28") = NumCel
End If

End Sub

Browser other questions tagged

You are not signed in. Login or sign up in order to post.