Populate userform textbox based on vLookup value

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
Hi All, I want to populate a userform based on a value based on a vLookup formula. The textbox is not being populated, and I can't figure out why... I'm getting an error
Unable to get the vLookup property of the WorksheetFunction class
Here's the code:
VBA Code:
Private Sub cboSN_Change()
   
    Dim ws As Worksheet
    Dim sRange As Range
    Dim lRow As Long

    Set ws = Sheets("Lookup")
    lRow = ws.Cells(Rows.Count, "G").End(xlUp).Row
    Set sRange = ws.Range("G1:I" & lRow)

    Me.txtCustomer = Application.WorksheetFunction.VLookup(Me.cboSN, sRange, 3, False)
    Me.txtModel = Application.WorksheetFunction.VLookup(Me.cboSN, sRange, 2, False)

End Sub

...any ideas?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I'm getting an error

The error is because the data to search does not exist. I recommend using the Find method.
This way you can control when the data does not exist:

VBA Code:
Private Sub cboSN_Change()
  Dim ws As Worksheet
  Dim sRange As Range, f As Range
  Dim lRow As Long

  Set ws = Sheets("Lookup")
  lRow = ws.Cells(Rows.Count, "G").End(xlUp).Row
  Set sRange = ws.Range("G1:I" & lRow)
  
  'Use the find method to find the data.
  Set f = sRange.Find(Me.cboSN, , xlValues, xlWhole, , , False)
  
  If Not f Is Nothing Then
    Me.txtCustomer = f.Offset(0, 2).Value       'You can use offset to the right.
    Me.txtModel = ws.Range("H" & f.Row).Value   'Or you can directly use the cell.
  Else
    MsgBox "Does not exist"
  End If
End Sub


Here you can see the advantages of using the Find method.
(please use the subtitles in your language)
 
Upvote 0
Solution
The error is because the data to search does not exist. I recommend using the Find method.
This way you can control when the data does not exist:

VBA Code:
Private Sub cboSN_Change()
  Dim ws As Worksheet
  Dim sRange As Range, f As Range
  Dim lRow As Long

  Set ws = Sheets("Lookup")
  lRow = ws.Cells(Rows.Count, "G").End(xlUp).Row
  Set sRange = ws.Range("G1:I" & lRow)
 
  'Use the find method to find the data.
  Set f = sRange.Find(Me.cboSN, , xlValues, xlWhole, , , False)
 
  If Not f Is Nothing Then
    Me.txtCustomer = f.Offset(0, 2).Value       'You can use offset to the right.
    Me.txtModel = ws.Range("H" & f.Row).Value   'Or you can directly use the cell.
  Else
    MsgBox "Does not exist"
  End If
End Sub


Here you can see the advantages of using the Find method.
(please use the subtitles in your language)
Thanks for your help. It's working fine, e a sua espanol esta muy entendible para mi, quien habla Portuguese como mia segunda idioma. 😉
 
Upvote 0
Thanks for your help. It's working fine
The marked solution post has been changed accordingly.

@wpryan: In your future questions, please mark the post as the solution that answered your question instead of your feedback post. This way, it will be more helpful to future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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