Dependant Text Box value

karolina1406

Board Regular
Joined
Apr 18, 2016
Messages
110
Office Version
  1. 365
Platform
  1. Windows
Can someone help in writing a code for a USer Form?

I have a table called "Products" with Product codes and Quantity. I have ComboBox1 with the unique product name and I want Textbox1 to be populated with the Quantity from the "Products" table based on the ComboBox1. Kind of vlookup formula in vba

any help much appreciated
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try this

VBA Code:
Private Sub ComboBox1_Change()
  TextBox1 = ""
  If ComboBox1 = "" Or ComboBox1.ListIndex = -1 Then Exit Sub
  TextBox1 = WorksheetFunction.VLookup(ComboBox1.Value, Range("Products"), 2, 0)
End Sub
 
Upvote 0
Try this

VBA Code:
Private Sub ComboBox1_Change()
  TextBox1 = ""
  If ComboBox1 = "" Or ComboBox1.ListIndex = -1 Then Exit Sub
  TextBox1 = WorksheetFunction.VLookup(ComboBox1.Value, Range("Products"), 2, 0)
End Sub
that's lovely, thank you very much!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
sorry to bother you but i opened the spreadsheet today and i suddenly got a Run-time error 1004 : Unable to get Vlookup property of the WorksheetFunction class. Any idea why??
 
Upvote 0
That is because the combo data does not exist in the range.

Try this:
VBA Code:
Private Sub ComboBox1_Change()
  Dim res As Variant
  TextBox1 = ""
  If ComboBox1 = "" Or ComboBox1.ListIndex = -1 Then Exit Sub
  On Error Resume Next
  res = Application.VLookup(ComboBox1.Value, Range("Products"), 2, 0)
  If IsError(res) Then
    MsgBox "No match"
  Else
    TextBox1 = res
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,855
Members
449,096
Latest member
Erald

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