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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,272
Members
448,558
Latest member
aivin

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