malcolm811
New Member
- Joined
- Jan 3, 2019
- Messages
- 1
Hi everyone,
I have a problem with a Macro that checks if certain cells contain data before the workbook can be saved.
I copied the VBA code from the Microsoft online documentation about the Workbook.BeforeSave Event (Excel) and I cannot get it to work if working at Workbook level.
The VBA code is simply
Sheet1 is the name of the worksheet I have in my Excel file.
If I use the code in a Worksheet it works just fine. For example
I need Workbook_BeforeSave though and when saving Workbook_BeforeSave I get the message Error 1004 “Application-defined or Object-defined error".
works too but I do not want to check the whole range from D5 to D15 but only those 6 cells.
Can somebody please help me?
Thanks in advance
I have a problem with a Macro that checks if certain cells contain data before the workbook can be saved.
I copied the VBA code from the Microsoft online documentation about the Workbook.BeforeSave Event (Excel) and I cannot get it to work if working at Workbook level.
The VBA code is simply
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If WorksheetFunction.CountA(Worksheets("Sheet1").Range("D5,D7,D9,D11,D13, D15")) < 6 Then
MsgBox "Workbook will not be saved unless" & vbCrLf & _
"All required fields have been filled in!"
Cancel = True
End If
End Sub
Sheet1 is the name of the worksheet I have in my Excel file.
If I use the code in a Worksheet it works just fine. For example
Code:
Private Sub Worksheet_Deactivate()
If WorksheetFunction.CountA(Worksheets("Sheet1").Range("D5,D7,D9,D11,D13, D15")) < 6 Then
MsgBox "Workbook will not be saved unless" & vbCrLf & _
"All required fields have been filled in!"
Cancel = True
End If
End Sub
I need Workbook_BeforeSave though and when saving Workbook_BeforeSave I get the message Error 1004 “Application-defined or Object-defined error".
Code:
If WorksheetFunction.CountA(Worksheets("Sheet1").Range("D5:D15")) < 6 Then
Can somebody please help me?
Thanks in advance