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

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I have checked the code twice before I posted it. I ran the code from a standard module.
Yes you are right, thanks for the answer, really appreciate your efforts also apologize for my comments. Thanks a lot.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.