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
276
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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,196
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
276
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,196
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
276
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
276
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,102,645
Messages
5,488,077
Members
407,623
Latest member
Deigs

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top