populate values in textbox based on optionbutton doesn't work

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
174
Office Version
  1. 2019
Platform
  1. Windows
Hello
I try to populate values in textbox based on select optionbuttonS after select combobox.
so should brings the values from cloumn F when select combobox1 and optionbutton1 into textbox1,and if I select combobox1 and optionbutton2 should brings the values from cloumn G and populate into textbox1.


VBA Code:
Private Sub ComboBox1_Change()
Dim c As Range
With Sheets("BS")
  
    Set c = .Range("B:B").Find(What:=ComboBox1.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If OptionButton1.Value = True Then
    If Not c Is Nothing Then
        ComboBox2.Value = c.Offset(, 1).Value
        ComboBox3.Value = c.Offset(, 2).Value
        ComboBox4.Value = c.Offset(, 3).Value
        TextBox1.Value = c.Offset(, 5).Value
        ElseIf OptionButton2.Value = True Then
        If Not c Is Nothing Then
        ComboBox2.Value = c.Offset(, 1).Value
        ComboBox3.Value = c.Offset(, 2).Value
        ComboBox4.Value = c.Offset(, 3).Value
        TextBox1.Value = c.Offset(, 6).Value
        End If
    End If
    End If
End With

End Sub
How can I do that ,please
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You are having some difficulty with the If-Then-Else constructs. The logic as shown here is not going to work as you want it to.

The only time to code in the outer If then is executed is when OptionButton1 is True. But OptionButton2 is set it will never be tested since that If Then is contained inside the OptionButton1 test.
 
Upvote 0
so what's the right procedure?

See if this update to your code does what you want

Rich (BB code):
Private Sub ComboBox1_Change()
    Dim c       As Range, rng As Range
    Dim search  As String
    
    Set rng = ThisWorkbook.Worksheets("BS").Range("B:B")
    
    search = Me.ComboBox1.Value
    
    Set c = rng.Find(What:=search, LookIn:=xlValues, LookAt:=xlWhole, _
                     SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, _
                    SearchFormat:=False)
                    
    If Not c Is Nothing Then
        'search value found
        Me.ComboBox2.Value = c.Offset(, 1).Value
        Me.ComboBox3.Value = c.Offset(, 2).Value
        Me.ComboBox4.Value = c.Offset(, 3).Value
        
        If Me.OptionButton1.Value = True Then
            Me.TextBox1.Value = c.Offset(, 5).Value
        ElseIf Me.OptionButton2.Value = True Then
            Me.TextBox1.Value = c.Offset(, 6).Value
        End If
        
        'if you only have two option buttons then above section in RED probably could be replaced with this line
        'Me.TextBox1.Value = c.Offset(, IIf(Me.OptionButton1.Value, 5, 6)).Value
        
    Else
        'optional search value not found message
        'MsgBox search & Chr(10) & "Record Not Found", 48, "Not Found"
        
    End If
    
End Sub

Dave
 
Upvote 0
thanks Dave,
unfortunately the code doesn't work well .
when I press optionbutton1 and combobox1 will brings the values from column G
press optionbutton1 should brings values from column F and optionbutton2 should brings values from column G with considering when move from optionbutton to another when select one of them , then should change value in textbox1 automatically .
 
Upvote 0
thanks Dave,
unfortunately the code doesn't work well .
when I press optionbutton1 and combobox1 will brings the values from column G
press optionbutton1 should brings values from column F and optionbutton2 should brings values from column G with considering when move from optionbutton to another when select one of them , then should change value in textbox1 automatically .

code only updated your ComboBox1_Change event code - you have not shared any other codes for your OptionButtons

Can suggest you publish ALL the codes in your userform

Dave
 
Upvote 0
I don't have others codes except this
VBA Code:
Private Sub OptionButton1_Click()
    If Me.OptionButton1 Then
        If Me.ComboBox1.ListIndex > -1 Then ComboBox1_Change
    End If
End Sub

Private Sub OptionButton2_Click()
    If Me.OptionButton2 Then
        If Me.ComboBox1.ListIndex > -1 Then ComboBox1_Change
    End If
End Sub

VBA Code:
Private Sub UserForm_Activate()
ComboBox1.RowSource = "BS!" & Sheets("BS").Range("B2", Sheets("BS").Range("B65536").End(xlUp)).Address

End Sub
 
Upvote 0
Then the offset values published in your original code, niether are not correct

change to the values shown in bold below & see if this now does what you want

Rich (BB code):
If Me.OptionButton1.Value = True Then
            Me.TextBox1.Value = c.Offset(, 4).Value
        ElseIf Me.OptionButton2.Value = True Then
            Me.TextBox1.Value = c.Offset(, 5).Value
        End If

Dave
 
Upvote 0
Solution
very strange ! and now I'm so confused how this works now . the matching based on column B and the values into column F,G

is the column B should be first column then the columns F,G should be 5,6 ?

last thing it doesn't change the values automatiaclly when move from optionbutton to another . any simple procedure to overcome this problem?
 
Upvote 0
is the column B should be first column then the columns F,G should be 5,6 ?
you count from the search column so column C = 1, D = 2 on so on
last thing it doesn't change the values automatiaclly when move from optionbutton to another . any simple procedure to overcome this problem?
Using your code, Textbox value is returned from Column F for optionbutton1 & Column G for optionbutton2
but this assumes that combobox1 has a value that can be found in the search range.

Dave
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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