Single Cells with Range on Workbook BeforeSave

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


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
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
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Welcome to the Board!

It seems to work fine for me. You are putting it in the "ThisWorkbook" module, right?

What happens if you change it to this, which should return a Message Box returning the count:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim ct As Long
    
    ct = WorksheetFunction.CountA(Worksheets("Sheet1").Range("D5,D7,D9,D11,D13,D15"))
    MsgBox "Count is: " & ct

    If ct < 6 Then
        MsgBox "Workbook will not be saved unless" & vbCrLf & _
        "All required fields have been filled in!"
        Cancel = True
    End If
    
End Sub
If that does not work, try adding this line above the "ct" calculation:
Code:
Sheets("Sheet1").Activate
If that still does not work, tell us exactly what happens when you try these steps, and let us know what version of Excel you are using.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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