What checkbox and/or optionbutton has been selected inside a specific frame

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
On my userform, I have 10 frames, each containing checkboxes and option buttons. Some checkboxes control whether or not a textbox is enabled or not. I would like to loop through each frame and see what is selected and assign .caption to a string variable separated by a ",". How can I do this?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this:

VBA Code:
Private Sub CommandButton1_Click()
  Dim ctrl As MSForms.Control
  Dim strVar As String
  
  For Each ctrl In Me.Controls
  If TypeName(ctrl) = "CheckBox" Or TypeName(ctrl) = "OptionButton" Then
      If ctrl Then strVar = strVar & ctrl.Name & ", "
    End If
  Next
  If strVar <> "" Then MsgBox Left(strVar, Len(strVar) - 2)
End Sub
 
Upvote 0
That worked but can this code be modified display message per frame. I want this code to do exactly what you created except I need the message box to appear after it looks inside each frame. Only display message box if something is checked.
Example.
If checkbox.value = true inside Frame1 then strVar = strVar & ctrl.Name & ", "
MsgBox Left(strVar, Len(strVar) - 2)

If checkbox.value = true inside Frame2 then strVar = strVar & ctrl.Name & ", "
MsgBox Left(strVar, Len(strVar) - 2)

....

If checkbox.value = true inside Frame10 then strVar = strVar & ctrl.Name & ", "
MsgBox Left(strVar, Len(strVar) - 2)

If the code below would work(which it does not) then that would be perfect. I changed the For Each ctrl In Me.Controls to For Each Frame In Me.Controls.

VBA Code:
Dim ctrl As MSForms.Control
  Dim strVar As String

  For Each FRAME In Me.Controls
  If TypeName(ctrl) = "CheckBox" Or TypeName(ctrl) = "OptionButton" Then
      If ctrl Then strVar = strVar & ctrl.Caption & ", "
    End If
  Next
  If strVar <> "" Then MsgBox Left(strVar, Len(strVar) - 2)

Thank You
 
Upvote 0
But you want a msgbox for each Frame

Or a msgbox that indicates all the frames and their controls, for example:

VBA Code:
Private Sub CommandButton1_Click()
  Dim ctrl As MSForms.Control
  Dim strVar As String
  
  For Each ctrl In Me.Controls
  If TypeName(ctrl) = "CheckBox" Or TypeName(ctrl) = "OptionButton" Then
      If ctrl Then strVar = strVar & ctrl.Parent.Name & ": " & ctrl.Name & vbCr
    End If
  Next
  If strVar <> "" Then MsgBox Left(strVar, Len(strVar) - 2)
End Sub
 
Upvote 0
Try this:

VBA Code:
Private Sub CommandButton1_Click()
  Dim ctrl As MSForms.Control, ctrlFrm As MSForms.Control
  Dim strVar As String
  Dim frm As MSForms.Frame
  
  For Each ctrl In Me.Controls
    strVar = ""
    If TypeName(ctrl) = "Frame" Then
      Set frm = ctrl
      For Each ctrlFrm In frm.Controls
        If TypeName(ctrlFrm) = "CheckBox" Or TypeName(ctrlFrm) = "OptionButton" Then
          If ctrlFrm Then strVar = strVar & ctrlFrm.Name & ", "
        End If
      Next
      If strVar <> "" Then MsgBox Left(strVar, Len(strVar) - 2), vbInformation, ctrl.Name
    End If
  Next
  
End Sub
 
Upvote 0
I did a simple loop in a loop:

Code:
Private Sub CommandButton1_Click()
  Dim fra As MSForms.Control
  For Each fra In Me.Controls
    If TypeOf fra Is MSForms.Frame Then
      Dim ctrl As MSForms.Control
      Dim sMessage As String
      sMessage = fra.Caption & vbNewLine
      For Each ctrl In fra.Controls
        If TypeOf ctrl Is MSForms.CheckBox Or TypeOf ctrl Is MSForms.OptionButton Then
          If ctrl.Value Then
            sMessage = sMessage & ctrl.Caption & ", "
          End If
        End If
      Next
      sMessage = Left$(sMessage, Len(sMessage) - 2)
      MsgBox sMessage
    End If
  Next
End Sub

Simple programming hint: Don't assume the default property of an object. Dante used

If ctrl Then

but I've used the more verbose

If ctrl.Value Then

which is potentially more readable and robust against the default property somehow changing.

Some people go as far as

If ctrl.Value = True Then

but that's like saying

If True = True Then
 
Upvote 0
Simple programming hint: Don't assume the default property of an object. Dante used
Thanks for the advice, I consider it in the next version and also correct .Name by .Caption:

VBA Code:
Private Sub CommandButton1_Click()
  Dim ctrl As MSForms.Control, cf As MSForms.Control
  Dim s As String
  For Each ctrl In Me.Controls
    s = ""
    If TypeName(ctrl) = "Frame" Then
      For Each cf In ctrl.Controls
        If "CheckBoxOptionButton" Like "*" & TypeName(cf) & "*" Then If cf.Value Then s = s & cf.Caption & ", "
      Next
      If s <> "" Then MsgBox Left(s, Len(s) - 2), vbInformation, ctrl.Name
    End If
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,807
Messages
6,121,679
Members
449,047
Latest member
notmrdurden

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