How to disable all buttons in a userform?

Montague26

New Member
Joined
Dec 6, 2015
Messages
14
I have 20+buttons in a userform, how can I disable all buttons by a automatic sub?

Only buttons
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
By buttons you mean:
Option Buttons
Toggle Buttons
Command Buttons
Spin Buttons
Is that what you mean?
 
Upvote 0
And when do you want this to happen? When you open the UserForm?
I'm not sure I understand your quote:"by a automatic sub"
 
Last edited:
Upvote 0
You can loop through the Controls like this:

Code:
Private Sub UserForm_Initialize()
    Dim Ctrl As Control
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "CommandButton" Then
            Ctrl.Enabled = False
         End If
    Next Ctrl
End Sub
 
Last edited:
Upvote 0
With some help from Andrew. If you want to disable all "buttons" you can use this script:
Code:
Private Sub UserForm_Initialize()

    Dim Ctrl As Control
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "CommandButton" _
        Or TypeName(Ctrl) = "OptionButton" _
        Or TypeName(Ctrl) = "SpinButton" _
        Or TypeName(Ctrl) = "ToggleButton" Then
        Ctrl.Enabled = False
         End If
    Next Ctrl
End Sub
 
Upvote 0
With some help from Andrew. If you want to disable all "buttons" you can use this script:
Code:
Private Sub UserForm_Initialize()

    Dim Ctrl As Control
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "CommandButton" _
        Or TypeName(Ctrl) = "OptionButton" _
        Or TypeName(Ctrl) = "SpinButton" _
        Or TypeName(Ctrl) = "ToggleButton" Then
        Ctrl.Enabled = False
         End If
    Next Ctrl
End Sub
You should be able to do your code this way as well...
Code:
Private Sub UserForm_Initialize()
  Dim Ctrl As Control
  For Each Ctrl In Me.Controls
    If TypeName(Ctrl) Like "*Button" Then Ctrl.Enabled = False
  Next
End Sub
 
Last edited:
Upvote 0
With some help from Andrew. If you want to disable all "buttons" you can use this script:
Code:
Private Sub UserForm_Initialize()

    Dim Ctrl As Control
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "CommandButton" _
        Or TypeName(Ctrl) = "OptionButton" _
        Or TypeName(Ctrl) = "SpinButton" _
        Or TypeName(Ctrl) = "ToggleButton" Then
        Ctrl.Enabled = False
         End If
    Next Ctrl
End Sub
Thanks, I solve it.
 
Upvote 0

Forum statistics

Threads
1,223,098
Messages
6,170,100
Members
452,301
Latest member
QualityAssurance

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