Populate TextBox with value dependant on OptionButton selection

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,736
Office Version
  1. 2007
Platform
  1. Windows
On my userform i have 3 option buttons1,2,3
I also have TextBox4

Depending on which OptionButton is selected depends on the text to be placed in TextBox4
Option Buttons & TextBox are in a frame if that helps.
Ive tried the below but nothing enters the Textbox

Rich (BB code):
Private Sub Worksheet_Change()
If OptionButton1.Value = True Then TextBox4.Value = "vbOKOnly": OptionButton1.Value = True
If OptionButton2.Value = True Then TextBox4.Value = "vbOKCancel": OptionButton2.Value = True
If OptionButton3.Value = True Then TextBox4.Value = "vbYesNo": OptionButton3.Value = True
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Why are you using worksheet change event? Are these controls in a sheet or a UserForm?
 
Upvote 0
Try this:
VBA Code:
Private Sub OptionButton1_Click()
  TextBox4.Text = ActiveControl.Caption
End Sub
Private Sub OptionButton2_Click()
  TextBox4.Text = ActiveControl.Caption
End Sub
Private Sub OptionButton3_Click()
  TextBox4.Text = ActiveControl.Caption
End Sub
 
Upvote 0
Hi,Am i supposed to be chaing the word Caption as when i select optionbutton 1 nothing is placed in the textbox
 
Upvote 0
This worked for me

Rich (BB code):
Private Sub OptionButton1_Click()
If OptionButton1 = True Then
TextBox4.TEXT = "vbOK"
End If
End Sub
 
Upvote 0
Solution
On my userform i have 3 option buttons1,2,3
I also have TextBox4

Rich (BB code):
Private Sub Worksheet_Change()
If OptionButton1.Value = True Then TextBox4.Value = "vbOKOnly": OptionButton1.Value = True
If OptionButton2.Value = True Then TextBox4.Value = "vbOKCancel": OptionButton2.Value = True
If OptionButton3.Value = True Then TextBox4.Value = "vbYesNo": OptionButton3.Value = True
End Sub

If this is a userform as you have said, you cannot use the Worksheet_Change event like that. Instead, you should use the userform's code module.

Simple example:
VBA Code:
'Code module for UserForm
Option Explicit

Private Sub UserForm_Activate()
    Call Macro2
End Sub

Private Sub OptionButton1_Click()
Call Macro2
End Sub

Private Sub OptionButton2_Click()
    Call Macro2
End Sub

Private Sub OptionButton3_Click()
    Call Macro2
End Sub


Private Sub Macro2()
'
' Macro2 Macro
'
    Dim ST As Style
    Dim WS As Worksheet
   
    With UserForm1
        If .OptionButton1.Value = True Then
            .TextBox4.Value = "vbOKOnly"
            .OptionButton1.Value = True
        ElseIf .OptionButton2.Value = True Then
            .TextBox4.Value = "vbOKCancel"
            .OptionButton2.Value = True
        ElseIf .OptionButton3.Value = True Then
            .TextBox4.Value = "vbYesNo"
            .OptionButton3.Value = True
        Else
            .TextBox4.Value = "vbOKOnly"
            .OptionButton1.Value = True
        End If
    End With
   
End Sub
 
Upvote 0
Thanks for the reply,i was unable to click the Mark as solution before so post #8 worked for me.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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