vlookup help

jdhogan0721

New Member
Joined
Oct 1, 2019
Messages
6
I am fairly new to vba and thought I could take on a small userform. Joke is on me, a simple vlookup is kicking my butt.

I have a userform that I simply want to use to lookup data from my spreadsheet. I don't need to edit anything, just display data in textboxes.

Static named range is - master

In column "A" I have a store number, when I enter the store number I want the details to populate into textboxes on the userform.

I have a button cleverly named gobutton that I want to click and have it lookup the store number and populate the boxes.

To save myself the hassle of naming a bunch of boxes I simply called each text box "box1" "box2" and so on.

box1 is the textbox I will use to input the store number to be searched.

box2 - box14 are for their corresponding columns. ex: box2 is for column b, box3 for column c and so on.

Here is what I have tried so far (i don't have the file on this computer so I can't copy and paste, bear with me).

Private Sub gobutton_click

With Me

.box2 = application.WorksheetFunction..VLookup(CLng(me.box1), Sheet1.Range("master"), 2, 0)

End With

End Sub

Not sure what I'm missing - getting an object required error

Any help is appreciated!!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi & welcome to MrExcel.
Rather than using a textbox for the store number, how about using a combobox? It would make the coding a lot easier
Are you open to that idea?
 
Upvote 0
The issue is that I have about 300 store numbers in my table and thought that may not be as easy to use. Open to ideas though.
 
Upvote 0
Ok, that's fair enough.
With a combo called ComboBox1 you could use this code
Code:
Private Sub ComboBox1_Click()
   Dim i  As Long
   
   For i = 1 To 14
      Me.Controls("box" & i).Value = Range("Master").Cells(Me.ComboBox1.ListIndex + 1, i + 1)
   Next i
End Sub

Private Sub UserForm_Initialize()
   Me.ComboBox1.List = Range("Master").Columns(1).Value
End Sub
 
Upvote 0
To continue using the Text boxes, try
Code:
   Dim Res As Variant
   With Me
      Res = Application.VLookup(CLng(.box1), Sheet1.Range("master"), 2, 0)
      If IsError(Res) Then
         .Box2 = "Not Found"
      Else
         .Box2 = Res
      End If
   End With
 
Upvote 0
sub/function not defined error. .box1 in the VLookup formula.

Am I correct in placing this as a sub for the go button click?
 
Upvote 0
As long as it's in the userform module then yes.
Are your boxes named Box1 Box 2 etc, or did you leave them as the standard TextBox1, TextBox2 etc?
 
Upvote 0
In that case this should work, as long as it's in the Userform Module
Code:
Private Sub gobutton_click()
   Dim Res As Variant
   With Me
      Res = Application.VLookup(CLng(.Box1), Sheet1.Range("master"), 2, 0)
      If IsError(Res) Then
         .Box2 = "Not Found"
      Else
         .Box2 = Res
      End If
   End With
End Sub
 
Last edited:
Upvote 0
This worked!! Thank you so much. I didn't realize the sheet name was different in the project editor. You are the best.
 
Upvote 0

Forum statistics

Threads
1,214,849
Messages
6,121,922
Members
449,056
Latest member
denissimo

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