User Form Option Button

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have a user form with several option buttons

After selecting an option button and clicking on Ok, I would like the button to show the last option button selected


it would be appreciated if someone could advise me how to do this
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
This is one way:-
If you add a textbox1 to your Userform it will show the previous Optionbutton as you select a new Optionbutton.
Paste code in Userform module and add code for further option buttons as per code:-
Code:
Option Explicit
[COLOR="Navy"]Dim[/COLOR] St1 [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] St2 [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Sub[/COLOR] Lpst(op [COLOR="Navy"]As[/COLOR] String)
[COLOR="Navy"]Dim[/COLOR] Temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
    [COLOR="Navy"]If[/COLOR] St1 = "" [COLOR="Navy"]Then[/COLOR]
        St1 = op
     Me.TextBox1.Text = "No Previous"
    [COLOR="Navy"]ElseIf[/COLOR] St2 = "" [COLOR="Navy"]Then[/COLOR]
        St2 = op
        Me.TextBox1.Text = St1
    [COLOR="Navy"]Else[/COLOR]
        Me.TextBox1.Text = St2
        St1 = St2
        St2 = op
    [COLOR="Navy"]End[/COLOR] If

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]


Private [COLOR="Navy"]Sub[/COLOR] OptionButton1_Change()
Call Lpst(OptionButton1.Name)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Private [COLOR="Navy"]Sub[/COLOR] OptionButton2_Change()
Call Lpst(OptionButton2.Name)

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Private [COLOR="Navy"]Sub[/COLOR] OptionButton3_Change()
Call Lpst(OptionButton3.Name)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]

Private [COLOR="Navy"]Sub[/COLOR] OptionButton4_Change()
Call Lpst(OptionButton4.Name)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
A similar but slightly better Option :-
Code:
Option Explicit
[COLOR="Navy"]Dim[/COLOR] NwOp [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]Sub[/COLOR] Lpst(Op [COLOR="Navy"]As[/COLOR] Object)
[COLOR="Navy"]If[/COLOR] NwOp [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
    Me.TextBox1.Text = "No Previous"
    [COLOR="Navy"]Set[/COLOR] NwOp = Op
[COLOR="Navy"]Else[/COLOR]
    Me.TextBox1.Text = NwOp.Name
    [COLOR="Navy"]Set[/COLOR] NwOp = Op
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]

Private [COLOR="Navy"]Sub[/COLOR] OptionButton1_Change()
 Lpst OptionButton1
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]

Private [COLOR="Navy"]Sub[/COLOR] OptionButton2_Change()
 Lpst OptionButton2
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]

Private [COLOR="Navy"]Sub[/COLOR] OptionButton3_Change()
 Lpst OptionButton3
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]

Private [COLOR="Navy"]Sub[/COLOR] OptionButton4_Change()
 Lpst OptionButton4
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Guys

Thanks for the reply. I have used Mick's 2nd option as I found it easier to follow and adapt

Mick, it works perfectly
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

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