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
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Write
Cancel = True instead of Exit Sub
 
Upvote 0
Solution
Write
Cancel = True instead of Exit Sub
You are a genius!!! Hey if I want the cell to be selected where the problem is should I put this before or after that line you gave me:

rngCell.Offset(0,-9).Select
 
Upvote 0
Try putting that before
Thank you. I am also wondering now since you are so helpful. With that working out, can it also automatically go to the selected cell instead of staying in the save as screen?
 
Upvote 0
Put SaveAsUI = False above cancel=true
😁
 
Upvote 0
Put SaveAsUI = False above cancel=true
😁
That didn't seem to work. When I went to save after the prompts came up it still stayed on the Save As Screen. If this isn't possible it is not a make or break thing. Just a nice to have thing
 
Upvote 0
That didn't seem to work. When I went to save after the prompts came up it still stayed on the Save As Screen. If this isn't possible it is not a make or break thing. Just a nice to have thing
I should've given you the rest of the code:

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"
rngCell.Offset(0, -9).Select
SaveAsUI = False
Cancel = True
End If

If rngCell.Value > "" And rngCell.Offset(0, -6).Value = "" Then
MsgBox "LOCATION missing on Extinguisher Sheet", vbInformation, "Dignity Fire Protection"
rngCell.Offset(0, -6).Select
SaveAsUI = False
Cancel = True
End If

If rngCell.Value > "" And rngCell.Offset(0, -2).Value = "" Then
MsgBox "SIZE missing on Extinguisher Sheet", vbInformation, "Dignity Fire Protection"
rngCell.Offset(0, -2).Select
SaveAsUI = False
Cancel = True
End If

If rngCell.Value > "" And rngCell.Offset(0, -1).Value = "" Then
MsgBox "TYPE missing on Extinguisher Sheet", vbInformation, "Dignity Fire Protection"
rngCell.Offset(0, -1).Select
SaveAsUI = False
Cancel = True
End If

If rngCell.Value = "Fail" And rngCell.Offset(0, 1).Value = "" Then
MsgBox "COMMENTS missing on Failed FE's - Extinguisher Sheet", vbInformation, "Dignity Fire Protection"
rngCell.Offset(0, 1).Select
SaveAsUI = False
Cancel = True
End If
Next


End Sub
 
Upvote 0
Ah ok and how about this? I just did it for the first part

VBA Code:
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"
  rngCell.Offset(0, -9).Select
  Cancel = True
  Exit For
End If
 
Upvote 0
Ah ok and how about this? I just did it for the first part

VBA Code:
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"
  rngCell.Offset(0, -9).Select
  Cancel = True
  Exit For
End If
Thanks for this one too... But it still keeps us on the Save as screen after prompts appear
 
Upvote 0

Forum statistics

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