VBA Error Viewing Data from a Worksheet

0

I'm experimenting with VBA and I've been able to add data to a DB on Sheet 2 .

And now I've tried to show the data on sheet 1 where I choose the ID and it shows the data inserted there, but it gives me an error.

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 error I have is

  

Run time error 1004
  Unable to get the VLookup property of the WorksheetFunction class

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

    
asked by anonymous 20.12.2017 / 15:11

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 worksheet 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
    
21.12.2017 / 11:22