0

I am trying to check all command button in a user form for enabled state and if state is false then enabling it. I have tried this code but runs without error but changes nothing where I manually disable button. It suppose to enable them. Would you please check the code. I get the code from this page https://stackoverflow.com/questions/49050223/vba-loop-through-buttons-on-userform

Sub form_reset()
    Dim ctrl1 As Control
        For Each ctrl1 In frmview.Controls
            If TypeName(ctrl1) = "commandbutton" Then
                Dim cmdbtn As CommandButton
                Set cmdbtn = ctrl
                If cmdbtn.Enabled = False Then
                    cmdbtn.Enabled = True
                End If
            End If
         Next
         frmview.Show
Deb
  • 121
  • 1
  • 1
  • 12

2 Answers2

0

Ok I have figure it out the problem with this code is it only work the button names like btn1, btn2 like. It will not checking the type of control thus making no changes. I was thinking of deleting this post but then think this might save someones time.

Sub form_reset()
Dim ctrl1 As Control
Dim cmdbtn As msforms.CommandButton
    For Each ctrl1 In frmview.Controls
        If TypeOf ctrl1 Is msforms.CommandButton Then
            Set cmdbtn = ctrl1
            If cmdbtn.Enabled = False Then
                cmdbtn.Enabled = True
            End If
        End If
     Next
     frmview.Show

End Sub

Deb
  • 121
  • 1
  • 1
  • 12
0

Try this code

Sub Form_Reset()
    Dim ctrl As Control
    For Each ctrl In frmview.Controls
        If TypeName(ctrl) = "CommandButton" Then ctrl.Enabled = True
    Next ctrl
    frmview.Show
End Sub
YasserKhalil
  • 9,138
  • 7
  • 36
  • 95
  • Yes, I have checked the code twice before I posted it. I ran the code from a standard module. – YasserKhalil Aug 28 '21 at 12:17
  • 1
    Yes you are right, thanks for the answer, really appreciate your efforts also apologize for my comments. Thanks a lot. – Deb Aug 28 '21 at 12:21