Clearing 'Groups' Of Userform Controls (avoiding clearing all userform controls, or doing each individually)

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
In my userform, I have 10 frames each with a variety of controls (textboxes, comboboxes, check boxes) in each specific to that frame. Some frames have as little as 4 controls, others as many as 15.

Is there an efficient way that I can clear the values of each of these controls in just the frames (not the entire form) rather than having to code each control's property individually? Ihave this pseudo code below, but I suspect I can't simply isolate groups of controls based on their unique frame.

Pseudo code:

Rich (BB code):
With Me
    For frm = 1 to 10
        nameFrm = ? frame name of frm(i) ?   'eg frm2d_setup_exp10
        for each control in nameFrm 'loop through each of the 12 controls in frm2d_setup_exp10
            if control is a textbox value = false
            if control is a combobox or textbox value = ""
        next
    Next frm
End With
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Note:
Frame names all share the prefix "frm2" with each having a unique letter (a-j) affixed. eg frm2a - frm2j
 
Upvote 0
Try . . .

VBA Code:
Private Sub CommandButton1_Click()

    Dim i As Long
    Dim frm As MSForms.Frame
    Dim ctrl As MSForms.Control
    Dim arr As Variant
    
    arr = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
    
    For i = LBound(arr) To UBound(arr)
        Set frm = Me.Controls("frm2" & arr(i) & "_setup_exp10") 'eg frm2d_setup_exp10
        For Each ctrl In frm.Controls
            Select Case TypeName(ctrl)
                Case "TextBox", "ComboBox"
                    ctrl.Value = ""
                Case "CheckBox"
                    ctrl.Value = False
            End Select
        Next ctrl
    Next i
    
End Sub

Hope this helps!
 
Last edited:
Upvote 0
ClearOneFrame gives good control. ClearAllFrames uses that.
VBA Code:
Sub ClearAllFrames()
    Dim aControl As MSForms.Control
    For Each aControl In Me.Controls
        If TypeName(aControl) = "Frame" Then
            Call ClearOneFrame(aControl)
        End If
    Next aControl
End Sub

Sub ClearOneFrame(aFrame As MSForms.Frame)
    Dim OneControl As MSForms.Control, i As Long
  
    For Each OneControl In aFrame.Controls
        With OneControl
        Select Case TypeName(OneControl)
            Case "ListBox", "ComboBox"
                    For i = 0 To .ListCount - 1
                        .Selected(i) = False
                    Next i
              
            Case "TextBox"
                .Value = vbNullString
              
            Case "OptionButton", "Checkbox", "ToggleButton"
                .Value = False
              
            Case "TabStrip", "MultiPage"
                .Value = 0
              
            Case "Frame"
                Call ClearOneFrame(OneControl)
          
        End Select
        End With
    Next OneControl
End Sub
 
Last edited:
Upvote 0
Solution
Hi,
try

VBA Code:
   Dim ctrl    As Control
    Dim i       As Long
   
    For i = 97 To 106
        For Each ctrl In Me.Controls("frm2" & Chr(i)).Controls
       
            ctrl.Value = IIf(VarType(ctrl) = vbBoolean, False, "")
       
        Next ctrl
    Next i

Dave
 
Upvote 0
whoops, omitted the bit at the end of the name

Rich (BB code):
    Dim ctrl    As Control
    Dim i       As Long
   
    For i = 97 To 106
        For Each ctrl In Me.Controls("frm2" & Chr(i) & "_setup_exp10").Controls
       
            ctrl.Value = IIf(VarType(ctrl) = vbBoolean, False, "")
       
        Next ctrl
    Next i
 
Upvote 0
Thank you all for your help. I really appreciate the different solutions that are available. I chose to test mikerickson's solution as it didn't rely on the full frame name. My original post was misleading. All 10 frames have a unique name, sharinng only the prefix f2. The letters are different, and in my example "_setup_exp10" are unique among the other frame names.

Mikerickson, I am getting an error in the ClearAllFrames procedure. "Invalid use of Me keyword".
I am calling this procedure from within my userform initialization code and the form hasn't been opened yet. Is this the cause of this error? If so, how would I overcome it?
 
Upvote 0
My original post was misleading. All 10 frames have a unique name, sharinng only the prefix f2. The letters are different, and in my example "_setup_exp10" are unique among the other frame names.

No worries & glad found solution - always helpful to forum if you fully share requirement.

Dave
 
Upvote 0
If your procedure is located in a regular module, you won't be able to refer to your userform using the Me keyword. You'll need to refer to it by name, for example . . .

VBA Code:
For Each aControl In UserForm1.Controls

Any reason, though, why the code isn't located within the userform code module?
 
Upvote 0
Hi Domenic, thank you for pointing that out. I put the two procedures back into the userform code module and things are working wonderfully.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,497
Members
448,967
Latest member
visheshkotha

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