How to tab to a specific control automatically once an option button inside a frame is selected

Pookiemeister

Active Member
Joined
Nov 26, 2015
Messages
268
Office Version
365, 2010
Inside a userform, I have a frame and a textbox. Inside the frame I have six option buttons. When the form initializes none of the option buttons are selected but as soon as the user selects any of the option buttons I need it to tab to the next control (textbox1). Is there not an easier way to do this? When I web searched this, one website stated that a class had to be created and then a whole bunch of code, which made no sense to me. Here is what I have so far. This code will work when assigned to a command button but I would like to avoid using a command button. I have tried using two different events for this to see how the results would be different and here they are:

The first way gave me a run time error 13: Type Mismatch.
Code:
Private Sub Frame1_Enter()    
Dim optBtn As OptionButton
    For Each optBtn In Me.Frame1.Controls
        If optBtn.Value = True Then
            MsgBox optBtn.Caption
        End If
    Next
End Sub
The second way doesn't do anything.
Code:
Private Sub Frame1_Click()

Dim optBtn As OptionButton
    For Each optBtn In Me.Frame1.Controls
        If optBtn.Value = True Then
            MsgBox optBtn.Caption
        End If
    Next
End Sub
The use of the msgbox in both examples is for test purposes only. Thank you all for any help given.
 
Last edited:

Some videos you may like

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,147
Place this code in the button_click event of each option button:
Code:
Dim ctrl As Control
For Each ctrl In Me.Controls
    If TypeOf ctrl Is msforms.OptionButton Then
        If ctrl = True Then
            TextBox1.SetFocus
        End If
    End If
Next
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
Try this for your test of controls:
Since you want script to run when you click on Frame:

Code:
Private Sub Frame1_Click()
'Modified  7/20/2019  5:04:38 PM  EDT
Dim c As Control
    For Each c In Me.Frame1.Controls
        If TypeOf c Is MSForms.OptionButton And c.Value = True Then MsgBox c.Caption
    Next
End Sub
 

Pookiemeister

Active Member
Joined
Nov 26, 2015
Messages
268
Office Version
365, 2010
Thank you for the quick response. This code only works when the user clicks the option button and then clicks anywhere inside the frame. Which makes sense because the code is inside the Frame1_Click event. Is there a way to this without clicking Frame1 after selecting an option button like a change event but for Frame1 (which unfortunately does not exist).
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,147
Did you try the macro I suggested in Post #2 ?
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
That is because the script looks to see if any Option Button is clicked. If no option button is true then their is no message to report.
Thank you for the quick response. This code only works when the user clicks the option button and then clicks anywhere inside the frame. Which makes sense because the code is inside the Frame1_Click event. Is there a way to this without clicking Frame1 after selecting an option button like a change event but for Frame1 (which unfortunately does not exist).
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
I thought you said you wanted script to run when you clicked on Frame.

Then I would suggest you use Mumps suggestion
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,762
Office Version
2013
Platform
Windows
Put these three scripts in your UserForm
You may need more depending on number of Option Buttons

Code:
Private Sub OptionButton2_Click()
Call SetMyFocus
End Sub
Private Sub OptionButton3_Click()
Call SetMyFocus
End Sub
Sub SetMyFocus()
'Modified  7/20/2019  5:37:36 PM  EDT
TextBox1.SetFocus
End Sub
 

Pookiemeister

Active Member
Joined
Nov 26, 2015
Messages
268
Office Version
365, 2010
#mumps
I did try the macro in post 2 and I responded; which would be in post 4. Thank you both for your help.
 

Pookiemeister

Active Member
Joined
Nov 26, 2015
Messages
268
Office Version
365, 2010
#My Aswer Is This
I was trying to avoid doing it that way. I was hoping for a universal solution than assigning code to each individual option buttons. So I guess this is the best way of doing what I need?
 

Watch MrExcel Video

Forum statistics

Threads
1,101,774
Messages
5,482,837
Members
407,365
Latest member
Leah Ashley

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top