How to stop a workbook being saved using VBA

ThomasB

Active Member
Joined
May 2, 2007
Messages
314
Hello All

I have a workbook with one worksheet with various cell to be filled in by various members of staff, then saved and e-mailed to myself.

What I would like to occur, if cells in the range A1:A10. are not filled in when the staff try to save the file is:

1) Prompt them to fill in this range with a message
2) Prevent them from saving the file until at least one of these cells are populated

Does anyone know how to do this in VBA?

Best Wishes

Thomas
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
Maybe this will get you started!!
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Application.WorksheetFunction.CountA(Range("$A$1:$A$10")) = 0 Then
MsgBox "Wrong"
Cancel = True
Range("$A$1").Select
End If
End Sub

This goes in the ThisWorrkbook module. RightClick the Excel icon to the left of the word '"File" on the menu bar and choose "View Code"

If each individual has a certain cell to fill in, you can use Select Case to customize the message and redirect to the relevent cell for that user. If that's what you need, post back with details.

lenze
 

QuietRiot

Well-known Member
Joined
May 18, 2007
Messages
1,079
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. MacOS
Code:
Private Sub WorkBook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Application.WorksheetFunction.CountA(Range("$A$1:$A$10")) = 0 Then
ActiveWorkbook.Saved = True
Cancel = True
SaveAsUI = False
MsgBox "Can NOT save incomplete data in Column A!"
Else
ActiveWorkbook.Saved = False
Cancel = False
SaveAsUI = True
End If
End Sub

not sure if the activeworkbook.saved and saveasUI is needed but this is what i came up with
 

ThomasB

Active Member
Joined
May 2, 2007
Messages
314
Stopping a workbook being saved on VBA Part 2

Many thanks to Lenze and Quiet Riot for their answer to my question both sets of code work beautifully I went with quietriots: (see below)

have a futher problem, as this file is to be uploaded onto a website, I cannot save the workbook to my own hard drive in order to e-mail out to the it department, when the range in question is unpopulated (which it needs to be) when it is uploaded onto the internet ), any ideas how I can get around this?

Private Sub WorkBook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Application.WorksheetFunction.CountA(Range("$A$1:$A$10")) = 0 Then
ActiveWorkbook.Saved = True
Cancel = True
SaveAsUI = False
MsgBox "Can NOT save incomplete data in Column A!"
Else
ActiveWorkbook.Saved = False
Cancel = False
SaveAsUI = True
End If
End Sub
 

Forum statistics

Threads
1,181,054
Messages
5,927,855
Members
436,573
Latest member
CMR237

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
Top