vlookup from a user form?

Mikal

Board Regular
Joined
Aug 31, 2015
Messages
73
I have a user form that I would like to modify the code on, It has a list box that pulls up a part#, then it has a text box for the cost of that part. In the text box I would like to do a vlookup that refers to the list box and pulls in the cost from a table. Currently the text box has this code.

Private Sub In_CostPerUnit_Change()
'Fill with PN In_CostPerUnit
In_CostPerUnit.Value = Range("U12").Value

is there code I can add to do a vlooup?

thanks in advance for your help
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Delete your code and try with the following.

I guess the parts# are in column T on sheet 1 and the costs in column U. Change the data in red for your data.
Code:
Private Sub ListBox1_Click()
  Dim f As Range
  Set f = Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Range("[COLOR=#ff0000]T:T[/COLOR]").Find(ListBox1.Value, , xlValues, xlWhole)
  If Not f Is Nothing Then
    In_CostPerUnit.Value = Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Cells(f.Row, "[COLOR=#ff0000]U[/COLOR]").Value
  End If
End Sub

It works like this, every time you select an item from the listbox, it automatically searches and fills the textbox.
 
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