I have a vba userForm that has 36 buttons on it. I would like to disable all buttons when a value on one of my spreadshets reaches a certain number. Right now with each click of a button a number goes up by one on the spreadsheet I'm referencing. When the number reaches three I would like to disable all buttons.
You can disable the <button> element in HTML by adding the disabled attribute to the element. The disabled attribute is a boolean attribute that allows you to disable an element, making the element unusable from the browser.
1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.
Here use setTimeout, to enable it after 30 seconds. In the anonymus function of setTimeout. Modify the DOM property is also called disabled and is a boolean that takes true or false.
Dim oCtrl As Control
Dim oButton As CommandButton
For Each oCtrl In MyForm.Controls
If oCtrl.Tag = "SomeValue" Then
Set oButton = oCtrl
oButton.Enabled = False
End If
Next
Set oCtrl = Nothing
Set oButton = Nothing
If you have other buttons which you do not want disabled, the solution is to use the Tag property. Set the Tag property on all the buttons which you will want to enable or disable together to the same value. Then you can check for that value in a look and enable/disable them. Another way is to name them the same prefix or suffix and check for that in your code.
Addition
Btw, the Control object does not have an Enabled property. So you must "cast" it to a CommandButton to disable it. Apparently a Control object does have an Enabled property but it does not show in intellisense. However, you should still try to cast the Control to a CommandButton to ensure that's what you have. Here's an expanded version:
Dim oCtrl As Control
Dim oButton As CommandButton
For Each oCtrl In MyForm.Controls
If oCtrl.Tag = "SomeValue" Then
On Error Resume Next
Set oButton = oCtrl
On Error GoTo 0
If Not oButton Is Nothing Then
oButton.Enabled = False
End If
End If
Next
Set oCtrl = Nothing
Set oButton = Nothing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With