vlookup to pull info into a text box based on a combo box value in userform

ollieb34

New Member
Joined
Jan 27, 2021
Messages
32
Office Version
  1. 2016
Platform
  1. Windows
Afternoon all,

I'm trying to pull a value from an adjacent cell within a table/range into a text box in a userform when the user selects an item from the combo box. See below code which I'm using that is returning a runtime error

1612628384867.png



Private Sub cmbProd_Change()

txtProd.Value = Application.VLookup(Me.cmbProd.Value, Sheets("Lookups").Range("C2:H17"), 2, 0)


End Sub

Any ideas as to what I could be doing wrong?

Thanks in advance
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
It means no value is found, so you need to trap the error.
Try:
VBA Code:
Private Sub cmbProd_Change()
Dim x As Variant
x = Application.VLookup(Me.cmbProd.Value, Sheets("Lookups").Range("C2:H17"), 2, 0)

If Not IsError(x) Then
    txtProd.Value = x
Else
    MsgBox "NOT FOUND"
End If

End Sub
 
Upvote 0
It means no value is found, so you need to trap the error.
Try:
VBA Code:
Private Sub cmbProd_Change()
Dim x As Variant
x = Application.VLookup(Me.cmbProd.Value, Sheets("Lookups").Range("C2:H17"), 2, 0)

If Not IsError(x) Then
    txtProd.Value = x
Else
    MsgBox "NOT FOUND"
End If

End Sub
Morning Akuni,
Thanks for the reply. tried the following code and every value which is selected from the combo box is now showing a msgbox "not found"

Does it matter that the data held within that combo box is a 6 digit number?

thanks
 
Upvote 0
Does it matter that the data held within that combo box is a 6 digit number?
Sorry for the late reply.
Try converting combobox value to Long type:
VBA Code:
    Private Sub cmbProd_Change()
    Dim x As Variant
    x = Application.VLookup(CLng(Me.cmbProd.Value), Sheets("Lookups").Range("C2:H17"), 2, 0)

    If Not IsError(x) Then
        txtProd.Value = x
    Else
        MsgBox "NOT FOUND"
    End If

    End Sub
 
Upvote 0
Solution
Sorry for the late reply.
Try converting combobox value to Long type:
VBA Code:
    Private Sub cmbProd_Change()
    Dim x As Variant
    x = Application.VLookup(CLng(Me.cmbProd.Value), Sheets("Lookups").Range("C2:H17"), 2, 0)

    If Not IsError(x) Then
        txtProd.Value = x
    Else
        MsgBox "NOT FOUND"
    End If

    End Sub
Morning Akuini,

No Problem, Yup thats the one... working fine now. Thank you very much for your support with this issue
 
Upvote 0
Morning Akuini,

No Problem, Yup thats the one... working fine now. Thank you very much for your support with this issue
Akuini,
Working fine to pull the data, however upon resetting the form either using the reset button or the save button now throwing a Runtime Error 13- Type mismatch

any ideas?

thanks
 
Upvote 0
however upon resetting the form either using the reset button or the save button now throwing a Runtime Error 13- Type mismatch
I think it's a different a problem, so you need to start a new thread.
 
Upvote 0
I think it's a different a problem, so you need to start a new thread.
the debug code links to the code you provided?

x = Application.VLookup(CLng(Me.cmbProd.Value), Sheets("Lookups").Range("C2:H17"), 2, 0)
 
Upvote 0
What do you mean by:
resetting the form either using the reset button or the save button

Do you put this line in other than Private Sub cmbProd_Change?
x = Application.VLookup(CLng(Me.cmbProd.Value), Sheets("Lookups").Range("C2:H17"), 2, 0)
 
Upvote 0
What do you mean by:
resetting the form either using the reset button or the save button

Do you put this line in other than Private Sub cmbProd_Change?
x = Application.VLookup(CLng(Me.cmbProd.Value), Sheets("Lookups").Range("C2:H17"), 2, 0)
When I save or reset the form it calls Sub Reset- which basically clears all the text boxes and fills the comboboxes ready.

iv also tried adding code to clear txtProd in sub reset- hoping that it would get round that error.

the above line of code you provided is only entered into CmbProd change
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,568
Members
449,237
Latest member
Chase S

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top