Stop file being saved

J

Jim Lavery

Is there an code that would stop an excel file being saved if a certain
number was exceeded.
Example:- entering leave which exceeds the quota thus stoppping the user
saving the file.
 
F

FSt1

hi
yes. but it would be nice to know which cell the leave is in and what the
quota is but in it's simplest form, it might look something like this.....
leave in A1. quota = 50
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Range("A1").Value > 50 Then
MsgBox "leave exceeds quota. save not allowed"
Cancel = True
End If
End Sub

this is workbook code.
Alt+F11. in the project window(far left) expand the project(file). double
click "ThisWorkbook". paste above code into this workbook module.
modify if needed.

Regards
FSt1
 
B

Bob Phillips

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
With Worksheets("Sheet1").Range("A1")

If .Value < 5 Then
msgbobx "Value must be >= 5"
Cancel = True
End If
End With
End Sub

'This is workbook event code.
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks),
'select View Code from the menu, and paste the code
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top