mardi 15 mai 2018

IF statement- error 1004 [Vlookup file returns zero]

I am creating a product database with data from excel Column (2nos). 1 column contains product description and another price.

A combo-box gets filled in with the products data (search as you type) and when a button is clicked, a textbox displays the price.

So the problem now is that, sometimes there might be products which is not on the database, and when button is clicked the program ends. tried to go around using IF function. dint work out as expected.

I am a noob to this. Please help.

 Dim ProductNa As Range
Dim ProducPr As Object
Private Sub LowPriceBtn_Click()
    Set ProductNa = Worksheets("Pdata").Range("A2:B7400")
    ProducPr = Application.WorksheetFunction.VLookup(Me.ProBox1.Value, ProductNa, 2, False)
    If ProducPr <> 0 Then
    proRate1.Text = ProducPr
    Else
    proRate1.Text = "Nothing available in database"
    End If
    End Sub

This code doesnt work.

This is the one which used to work.

Private Sub LowPriceBtn_Click()
Set ProductNa = Worksheets("Pdata").Range("A2:B7400")
proRate1.Text = Application.WorksheetFunction.VLookup(Me.ProBox1.Value, ProductNa, 2, False)

This does work. But when Vlookup returns no value, program crashes. PS: proRate1- Textbox name, probox1- combobox.

Aucun commentaire:

Enregistrer un commentaire