Code link optionbutton ,compobox with textbox and cells in sheet

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,494
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi, expert i would help finding code connect the data sheet with optionbutton , compobox and textbox in userform

for explanation i have data in sheet1 in columns c,d are two prices price the column b is the brand and userform 2 optionbuttons and compobox1, textbox1 here when i choose the brand in compox1 and choose optionbutton1 it gives me the price of column c and put in textbox1 into userform and if i choose optionbuttons2 it give me the price of column d and put in textbox1 into userform
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this:
Put this script in ComboBox1
Code:
Private Sub ComboBox1_Change()
'Modified  9/19/2019  12:22:01 PM  EDT
Dim ans As String
Dim SearchString As String
Dim SearchRange As Range
SearchString = ComboBox1.Value
Dim lastrow As Long
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Set SearchRange = Range("B1:B" & lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then MsgBox SearchString & "  Not Found": Exit Sub
If OptionButton1.Value = True Then ans = SearchRange.Offset(0, 1).Value
If OptionButton2.Value = True Then ans = SearchRange.Offset(0, 2).Value
TextBox1.Value = ans
End Sub
 
Upvote 0
Try this:
Put this script in ComboBox1
Code:
Private Sub ComboBox1_Change()
'Modified  9/19/2019  12:22:01 PM  EDT
Dim ans As String
Dim SearchString As String
Dim SearchRange As Range
SearchString = ComboBox1.Value
Dim lastrow As Long
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Set SearchRange = Range("B1:B" & lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then MsgBox SearchString & "  Not Found": Exit Sub
If OptionButton1.Value = True Then ans = SearchRange.Offset(0, 1).Value
If OptionButton2.Value = True Then ans = SearchRange.Offset(0, 2).Value
TextBox1.Value = ans
End Sub

thanks but there is problem when i press optionbutton2 it doesn't show price in textbox what in column d it stays price column c which connected optionbutton1
 
Upvote 0
I thought you wanted the script to run when you choose the item in the ComboBox

So choose the value in the ComboBox and then click the option Button.

Put these two scripts in the proper Option Buttons

Code:
Private Sub OptionButton1_Click()
'Modified  9/19/2019  4:49:01 PM  EDT
Dim ans As String
Dim SearchString As String
Dim SearchRange As Range
SearchString = ComboBox1.Value
Dim lastrow As Long
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Set SearchRange = Range("B1:B" & lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then MsgBox SearchString & "  Not Found": Exit Sub
If OptionButton1.Value = True Then ans = SearchRange.Offset(0, 1).Value
If OptionButton2.Value = True Then ans = SearchRange.Offset(0, 2).Value
TextBox1.Value = ans
End Sub
Private Sub OptionButton2_Click()
'Modified  9/19/2019  4:49:01 PM  EDT
Dim ans As String
Dim SearchString As String
Dim SearchRange As Range
SearchString = ComboBox1.Value
Dim lastrow As Long
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Set SearchRange = Range("B1:B" & lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then MsgBox SearchString & "  Not Found": Exit Sub
If OptionButton1.Value = True Then ans = SearchRange.Offset(0, 1).Value
If OptionButton2.Value = True Then ans = SearchRange.Offset(0, 2).Value
TextBox1.Value = ans
End Sub
 
Upvote 0
I thought you wanted the script to run when you choose the item in the ComboBox

So choose the value in the ComboBox and then click the option Button.

Put these two scripts in the proper Option Buttons

Code:
Private Sub OptionButton1_Click()
'Modified  9/19/2019  4:49:01 PM  EDT
Dim ans As String
Dim SearchString As String
Dim SearchRange As Range
SearchString = ComboBox1.Value
Dim lastrow As Long
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Set SearchRange = Range("B1:B" & lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then MsgBox SearchString & "  Not Found": Exit Sub
If OptionButton1.Value = True Then ans = SearchRange.Offset(0, 1).Value
If OptionButton2.Value = True Then ans = SearchRange.Offset(0, 2).Value
TextBox1.Value = ans
End Sub
Private Sub OptionButton2_Click()
'Modified  9/19/2019  4:49:01 PM  EDT
Dim ans As String
Dim SearchString As String
Dim SearchRange As Range
SearchString = ComboBox1.Value
Dim lastrow As Long
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Set SearchRange = Range("B1:B" & lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
If SearchRange Is Nothing Then MsgBox SearchString & "  Not Found": Exit Sub
If OptionButton1.Value = True Then ans = SearchRange.Offset(0, 1).Value
If OptionButton2.Value = True Then ans = SearchRange.Offset(0, 2).Value
TextBox1.Value = ans
End Sub
it doesn't show any price buddy!
 
Upvote 0
I have no other solution for you.
Maybe someone else here on the forum will have another solution.
You did not say what it did.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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