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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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