VBA Prevent save with checkboxes

crazybuckeyeguy

New Member
Joined
Apr 15, 2017
Messages
49
I am trying to set the following VBA to prevent save if any checkboxes are empty. Any help is great.

Private Sub dontsave()
If Sheets("Sheet1").Range("A3") = "" Then
Cancel = True
MsgBox "Save cancelled"
End If
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
If you use forms checkboxes, you could use this code.
Put this in the ThisWorkbook code module


Code:
' in ThisWorkbook code module

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cancel = Not (AllChecked)
    If Cancel Then MsgBox "save aborted"
End Sub

Function AllChecked(Optional SheetToCheck As Worksheet) As Boolean
    Dim oneShape As Object
    If SheetToCheck Is Nothing Then Set SheetToCheck = ThisWorkbook.ActiveSheet
    
    AllChecked = True
    For Each oneShape In SheetToCheck.Shapes
        With oneShape
            If .Type = msoFormControl Then
                If .FormControlType = xlCheckBox Then
                    AllChecked = AllChecked And (.ControlFormat.Value = xlOn)
                End If
            End If
        End With
    Next oneShape
End Function
 
Upvote 0
If you use forms checkboxes, you could use this code.
Put this in the ThisWorkbook code module


Code:
' in ThisWorkbook code module

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cancel = Not (AllChecked)
    If Cancel Then MsgBox "save aborted"
End Sub

Function AllChecked(Optional SheetToCheck As Worksheet) As Boolean
    Dim oneShape As Object
    If SheetToCheck Is Nothing Then Set SheetToCheck = ThisWorkbook.ActiveSheet
    
    AllChecked = True
    For Each oneShape In SheetToCheck.Shapes
        With oneShape
            If .Type = msoFormControl Then
                If .FormControlType = xlCheckBox Then
                    AllChecked = AllChecked And (.ControlFormat.Value = xlOn)
                End If
            End If
        End With
    Next oneShape
End Function

It worked perfectly. Thank you so much.
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

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