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

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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
 
Upvote 0
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
 
Upvote 0
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).
 
Upvote 0
Did you try the macro I suggested in Post #2 ?
 
Upvote 0
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).
 
Upvote 0
I thought you said you wanted script to run when you clicked on Frame.

Then I would suggest you use Mumps suggestion
 
Upvote 0
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
 
Upvote 0
#mumps
I did try the macro in post 2 and I responded; which would be in post 4. Thank you both for your help.
 
Upvote 0
#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?
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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