Can't get VBA code to prevent "Save As" from working with this code

Chassee

New Member
Joined
Mar 21, 2023
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi There. I have a code below that should be preventing excel from a user being able to save as without a certain range of cells filled out based on the parameters in my code. I thought it would work just fine, but in testing it will show the message box and then save anyways. Please help. Thanks :)

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

Dim rngCell As Range

For Each rngCell In Range("J22:K51")
If rngCell.Value > "" And rngCell.Offset(0, -9).Value = "" Then
MsgBox "NEXT SVC DUE missing on Extinguisher Sheet", vbInformation, "Dignity Fire Protection"
Exit Sub
End If
Next

End Sub
 
Did you put this exit for in every part?
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Use this for testing. The SaveAs GUI is not appearing.
Probably your worksheet passed al the conditions, which leads to showing the SaveAs screen

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  If [A1] = "" Then
    MsgBox "test", vbOKOnly, "test"
    Application.Goto [A4]
    Cancel = True
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
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