Enable Button only if all the checkboxes are ticked - VBA

Wafee

Board Regular
Joined
May 27, 2020
Messages
104
Office Version
  1. 2013
Platform
  1. Windows
I have a vba code that has been assigned to a button named as "run report". Have some instructions with check boxes and what I am trying to do is to enable button only if all the checkboxes are ticked. I tried something like below but somehow it's not working. It seems something is wrong with declaration.

```
Sub buttonenable()
Dim B1 as button
Dim C1 as checkbox
Dim C2 as checkbox
Dim C3 as Checkbox
Set B1 = Thisworbook.Sheets("Home").Button("Run report")
Set C1 = Thisworbook.Sheets("Home").Checkbox("Checkbox1")

Set C2 = Thisworbook.Sheets("Home").Checkbox("Checkbox2")

Set C3 = Thisworbook.Sheets("Home").Checkbox("Checkbox3")

IF C1 = True and C2 = True and C3 = True Then
B1.Enable = True

Else
B1.enable = False

End If

End Sub()
```
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi welcome to forum

To enable your Report Button, each checkbox would need to call a code to validate selections all complete.
Another approach would be to place the test at top of your Report Button Code & if checkbox selection incomplete, display msgbox & exit sub

Something like following

VBA Code:
Sub RunReport()
Dim cb As CheckBox

'Loop Checkboxes
  For Each cb In ActiveSheet.CheckBoxes
    If cb.Value = xlOff Then
        MsgBox "Select All CheckBoxes To Continue", 48, "Selection Required"
        Exit Sub
    End If
  Next cb
  
  'rest of code

End Sub

I have assumed that your checkboxes are Form controls - If ActiveX different code will be needed

Hope Helpful

Dave
 
Upvote 0
Hi welcome to forum

To enable your Report Button, each checkbox would need to call a code to validate selections all complete.
Another approach would be to place the test at top of your Report Button Code & if checkbox selection incomplete, display msgbox & exit sub

Something like following

VBA Code:
Sub RunReport()
Dim cb As CheckBox

'Loop Checkboxes
  For Each cb In ActiveSheet.CheckBoxes
    If cb.Value = xlOff Then
        MsgBox "Select All CheckBoxes To Continue", 48, "Selection Required"
        Exit Sub
    End If
  Next cb
 
  'rest of code

End Sub

I have assumed that your checkboxes are Form controls - If ActiveX different code will be needed

Hope Helpful

Dave


Hi Dave,

Thank you so much that works perfectly fine and sorry as it took time try and get back due to some system issues.

Regards,
Wafee
 
Upvote 0
Hi Dave,

Thank you so much that works perfectly fine and sorry as it took time try and get back due to some system issues.

Regards,
Wafee

no worries - glad suggestion helped

many thanks for feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,249
Members
449,075
Latest member
staticfluids

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