I have this code, that refuses and cancels to save a workbook if a cell contains a specific text. the code is working fine, but if i want it to refuse saving if some cells in a range contains specific text then it is not working. my code below that is working:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Sheets("Sheet1").Range("A1").Value = "Fill in a comment" Then
Cancel = True
Response = MsgBox("Fill in a comment", vbCritical, "Error!")
End If
End Sub
the code that i tried to make it work but its not
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean
For Each c In Sheet1.Range("A1:A5000")
If c.Value = "Fill in a comment" Then
Cancel = True
Response = MsgBox("Fill in a comment", vbCritical, "Error!")
End If
Next
End Sub
do you guys have any ideas?
Try this instead rather than looping:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = Not Sheet1.Range("A1:A5000").Find("Fill in a comment") Is Nothing
If Cancel Then MsgBox "Fill in a comment", vbCritical, "Error!"
End Sub
It uses the .Find() method to look for "Fill in a comment" and if it isn't Nothing then Cancel is set to True
Then, we test the value of Cancel to see if we need to display the message box.
Doing it this way also makes sure that you don't get 500 message boxes if you have "Fill in a comment" written in 500 different cells in that range...
Related
I have an odd thing (I think) but probably doing something incorrectly here. I am trying to force the user to enter in certain information in excel. The msgbox comes up with the OK/Cancel buttons but when either is selected the workbook saves and exits. If they cancel then I want it to sit on the cell where the issue is. Any help would be appreciated
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'---------------------------
'-- Missing Employee Name --
'---------------------------
If Cells(5, 2).Value = Empty Then
If Not MsgBox("Employee Name missing! Pressing OK will exit without saving.", vbOKCancel, "The Mill") = vbOK Then
With Sheets("sheet1")
.Activate
.Cells(5, 2).Activate
End With
Cancel = True
Exit Sub
End If
End If
End Sub
I think the problem is that you aren't handling Workbook_BeforeClose, which triggers as you try to close the workbook.
So while the save is canceled, the close is not.
Moving everything to Workbook_BeforeClose and handling it there kind of works, until the user saves manually. So you will still have to handle that in the before save event.
This is an example:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Cells(5, 2).Value = Empty Then
If MsgBox("Employee Name missing! Pressing OK will exit without saving.", vbOKCancel, "The Mill") = vbOK Then
Application.DisplayAlerts = False
Application.Quit
Else
Sheets("Sheet1").Cells(5, 2).Activate
Cancel = True
End If
End If
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Cells(5, 2).Value = Empty Then
Cancel = True
MsgBox "Employee Name missing!"
Sheets("Sheet1").Cells(5, 2).Activate
End If
End Sub
The only caveat I have seen, is that Application.DisplayAlerts = False will affect other open workbooks.
I want to save as an excel file as .csv from Sheet2 (Sheet name changes) so I want excel to pop up a message if I try to save as the file from Sheet1. I've a code to pop up the message but I'm not sure how to apply if condition for this scenario. Thank you for your help!!
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean)
If SaveAsUI Then
MsgBox "Make sure you are on correct sheet"
End If
End Sub
This Should Work for you:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
If ActiveSheet.Name = "Sheet1" Then
MsgBox "Make sure you are on correct sheet"
SaveAsUI = False
End If
End If
End Sub
If you are on Sheet1, the Msgbox will pop and sheet won't be saved.
I found this code and it works perfectly. But when I hit close button, dialog is shown "Do you weant to save changes", and if I choose Yes, an error comes up that I don't have a value in a cell A. And then my file is automatically closed.
How to prevent this, and to stay in the document?
My code is:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If ActiveSheet.Range("A1").Value = "" Then
Cancel = True
Response = MsgBox("Please enter a value in A1", vbCritical, "Error!")
End If
End Sub
Have you tried using BeforeClose instead of BeforeSave?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ActiveSheet.Range("A1").Value = "" Then
Cancel = True
Response = MsgBox("Please enter a value in A1", vbCritical, "Error!")
End If
End Sub
Regards
This question already has answers here:
Making fields mandatory of a specific sheet on workbook save
(3 answers)
Closed 8 years ago.
I want to call this Sub in the Workbook_BeforeSave event:
Sub test()
If ActiveSheet.Range("A4") = "" Then
MsgBox ("Please fill in cell A4!")
Exit Sub
Else
End If
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
test
End Sub
When I click save the file is saved even if the cell A4 is blank.
You should set Cancel as the proper way to terminate the save event.
It would be better to have test as a function, so you can check the return value, and set cancel appropriately.
Function test() as Boolean
If ActiveSheet.Range("A4") = "" Then
Test = False
Else
Test = True
End If
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If test = False then Cancel=True
End Sub
Also, ActiveSheet is not appropriate, unless you have only one worksheet, and you have protected the workbook from having extra sheets added.
My suggested solution would be to check Sheets("MySheet").Range("A4").
Of course, the laziest way to do the test would be:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
cancel = (ActiveSheet.Range("A4") = "")
End Sub
where the test is done within the save procedure, and no sub tests are required.
Try:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call test
End Sub
Sub test()
If ActiveSheet.Range("A4") = "" Then
MsgBox ("Please fill in cell A4!")
End
End If
End Sub
Exit Sub merely terminates sub test(), while End completely stops code execution, thus preventing Workbook_BeforeSave() from running further.
if cancel is true, the saving will be blocked :
Sub Test(byref cancel as boolean)
If ActiveSheet.Range("A4") = "" Then
MsgBox ("Please fill in cell A4!")
cancel = true
Exit Sub
'Else
End If
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Test Cancel
End Sub
First of all, I'm a total Excel interop noob.
I'm trying to get a date from a cell and then set the title of the document before the document gets saved, to be the month of the date. This is my code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.Title = DateTime.Month(ThisWorkbook.Sheets("Sheet1").Cell("A10"))
End Sub
I'm not sure that anything is working. I set a breakpoint on the code, but I can't "run" it because it's not a macro, but an event handler, so I don't think the breakpoint is going to work. I don't get any errors. I don't even know that ThisWorkbook.Title is what I want and I'm not even sure about getting the month from the cell.
To change the 'Title' built in property in Excel:
ActiveWorkbook.BuiltinDocumentProperties("Title") = "My Title Name"
The title of the document is a "Built In" property - this is the info that appears when you right click on the file and look at the properties.
The name of the spreadsheet is set on save, so you will want to save the file with a new name if you want to see the date on the file itself
A code something like this should give you the result you desire:
(note that this code is VBA, so it may need some tweaking to work in interop.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim FilePath As String
Dim varName As String
On Error GoTo ErrorHandler
' This disables all Excel events.
Application.EnableEvents = False
' disable the default behaviour of the save like so:
Cancel = True
'you can leave this blank if you want it to save in the default directory
FilePath = "C:\The path\To\The File"
varName = Format(ThisWorkbook.Sheets("Sheet1").Cell("A10"),"mmmm")
ActiveWorkbook.SaveAs Filename:=FilePath & varName & ".xlsx"
ErrorExit:
' This makes sure events get turned back on again no matter what.
Application.EnableEvents = True
Exit Sub
ErrorHandler:
MsgBox "No value submitted - File Not Saved"
Resume ErrorExit
End Sub
I assume this would serve your purpose:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.Caption = MonthName(Month(ThisWorkbook.Sheets("Sheet1").Range("C10").Value))
End Sub
Another thing is:
This title would not be there next time you open the workbook. So:
Private Sub Workbook_Open()
Application.Caption = MonthName(Month(ThisWorkbook.Sheets("Sheet1").Range("C10").Value))
ThisWorkbook.Save
End Sub