User Form Option Button

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,381
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
 

NoSparks

Well-known Member
Joined
Mar 15, 2013
Messages
830
Office Version
2010
Platform
Windows
see Alex D solution to similar question posted here back in 2014
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
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
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
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
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,381
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
 

Forum statistics

Threads
1,082,316
Messages
5,364,483
Members
400,802
Latest member
RichBRich

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top