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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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