MSauce
MSauce

Reputation: 123

VBA - Prevent users from saving unless checkbox is checked

I'm using this macro in order to prevent a user from saving the workbook unless they accept the Terms and Conditions, annotated with a checkbox.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    MsgBox "Please accept the terms and conditions"
    Cancel = True

End Sub

But this macro prevents a user from saving indefinitely. How can I make it so the user has to check a checkbox in order to save?

Thanks!

Upvotes: 0

Views: 225

Answers (1)

DisplayName
DisplayName

Reputation: 13386

try with

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cancel = Not Worksheets(2).CheckBoxes(1).Value = 1
    If Cancel Then MsgBox "Please accept the terms and conditions"
End Sub

where I assumed:

  • Worksheets(2) is the worksheet with the relevant checkbox
  • relevant Checkbox is the first in the worksheet otherwise change accordingly to your actual case

Upvotes: 2

Related Questions