Search code examples
excelvbauserform

How to set property to all controls on a UserForm?


I need to set a property .Value & ForeColor to all controls on a UserForm from a macro outside the UserForm.

The userform is shown as vbModeless.

The below code without On Error Resume Next is producing

Run-time error '438': Object doesn't support this property or method

At ctrl.Value = False & ctrl.ForeColor = vbBlack.

If I use the not recommended On Error Resume Next, then the code works.

Sub Clear_All_Filter()
    On Error Resume Next
    Dim ctrl As control
    For Each ctrl In UserForm1.Controls
        ctrl.Value = False
        ctrl.ForeColor = vbBlack
    Next
End Sub

Solution

  • Most likely you have a label-control that doesn't support value. Check for the type of the control within your loop:

    Sub Clear_All_Filter()
       
        Dim ctrl As Control
          For Each ctrl In UserForm1.Controls
            If TypeName(ctrl) <> "Label" Then
                ctrl.Value = False
                ctrl.ForeColor = vbBlack
            End If
          Next
    End Sub