Macro only calls on the first Before Save, rather than before every save

SkywardPalm

Board Regular
Joined
Oct 23, 2021
Messages
61
Office Version
  1. 365
Platform
  1. Windows
I am trying to have this macro run before every save, to ensure that the two totals in question are matching. It only runs the first time I press ctrl+s

ThisWorkbook:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Call ExtendedTotalCheck

End Sub
DollarTotalCheck:
VBA Code:
Sub ExtendedTotalCheck()
    Application.EnableEvents = True
    Dim wsMaster As Worksheet, wsSUMMARY As Worksheet
    Dim extHeaderCell As Range, f As Range
    Dim masterTotal As Double

    Set wsMaster = ThisWorkbook.Sheets("Master")
    Set wsSUMMARY = ThisWorkbook.Sheets("SUMMARY")
  
    Set extHeaderCell = wsMaster.Range("1:1").Find("Extended")
    If Not extHeaderCell Is Nothing Then
            masterTotal = extHeaderCell.Offset(, 1).Value
            Set f = wsSUMMARY.Range("C228:D228").Find(masterTotal, , xlValues, xlWhole)
        If Not f Is Nothing Then
            Debug.Print "Extended Totals Match"
        Else
            MsgBox "Please check Extended Totals match!"
        End If
    End If
End Sub
 
Try:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.EnableEvents = True
    Dim wsMaster As Worksheet, wsSUMMARY As Worksheet
    Dim extHeaderCell As Range, f As Range
    Dim masterTotal As Double
    Set wsMaster = ThisWorkbook.Sheets("Master")
    Set wsSUMMARY = ThisWorkbook.Sheets("SUMMARY")
    Set extHeaderCell = wsMaster.Range("1:1").Find("Extended")
    masterTotal = extHeaderCell.Offset(, 1).Value
    Set f = wsSUMMARY.Range("C228:D228").Find(masterTotal, , xlValues, xlWhole)
    If Not f Is Nothing Then
        MsgBox "Extended Totals Match"
    Else
        MsgBox "Please check Extended Totals match!"
        Cancel = True
    End If
End Sub
I've changed the debug.print to msgbox. It also cancels the "save" if "Please check Extended Totals match!" is displayed.
Thank you, I am getting the error message "Run-time error '91': Object variable or With block variable not set" on the line below. We know the variable exists because it was running fine the previous initial saves. I'm not sure what is causing this issue
VBA Code:
    masterTotal = extHeaderCell.Offset(, 1).Value
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I tested the macro using some dummy data and it worked properly. Could you upload a copy of your file (de-sensitized if necessary) to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here.
 
Upvote 0
I tested the macro using some dummy data and it worked properly. Could you upload a copy of your file (de-sensitized if necessary) to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here.
Here is a link to the file de-sensitized. It will work the first time, but the extHeaderCell = Nothing on all the next saves, causing the error mentioned
 
Upvote 0
Try:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.EnableEvents = True
    Dim wsMaster As Worksheet, wsSUMMARY As Worksheet
    Dim extHeaderCell As Range, f As Range
    Dim masterTotal As Double
    Set wsMaster = ThisWorkbook.Sheets("Master")
    Set wsSUMMARY = ThisWorkbook.Sheets("SUMMARY")
    Set extHeaderCell = wsMaster.Rows(1).Find("Extended", LookIn:=xlFormulas, lookat:=xlWhole)
    masterTotal = extHeaderCell.Offset(, 1).Value
    Set f = wsSUMMARY.Range("C228:D228").Find(masterTotal, , xlValues, xlWhole)
    If Not f Is Nothing Then
        MsgBox "Extended Totals Match"
    Else
        MsgBox "Please check Extended Totals match!"
        Cancel = True
    End If
End Sub
 
Upvote 0
Solution
Try:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.EnableEvents = True
    Dim wsMaster As Worksheet, wsSUMMARY As Worksheet
    Dim extHeaderCell As Range, f As Range
    Dim masterTotal As Double
    Set wsMaster = ThisWorkbook.Sheets("Master")
    Set wsSUMMARY = ThisWorkbook.Sheets("SUMMARY")
    Set extHeaderCell = wsMaster.Rows(1).Find("Extended", LookIn:=xlFormulas, lookat:=xlWhole)
    masterTotal = extHeaderCell.Offset(, 1).Value
    Set f = wsSUMMARY.Range("C228:D228").Find(masterTotal, , xlValues, xlWhole)
    If Not f Is Nothing Then
        MsgBox "Extended Totals Match"
    Else
        MsgBox "Please check Extended Totals match!"
        Cancel = True
    End If
End Sub
Works like a charm! In the instance that the SUMMARY total is a formula "=ROUND(SUM(SUMMARY!$C$17:$C$226),2)" and the Master total is a different formula "=SUM(AA:AA)", even though they are $0 they do not equal and it will stop the save. Is there a way to compare the cell values as numbers (preferably two decimal .00)? Thanks for all of your help!
 
Upvote 0
The formulae in C228 and D228 in SUMMARY are the same. What is the reason they are the same?
 
Upvote 0
The formulae in C228 and D228 in SUMMARY are the same. What is the reason they are the same?
They should not be the same, I copied the formula from the current to the previous to provide an example of what could be there. the formula should represent the total value from the respective column that the totals are in. (The F column should read =ROUND(SUM(SUMMARY!$F$17:$F$226),2)* instead of C) Sorry for the confusion. The macro you provided works perfectly unless the totals equal 0, then the msgbox "Please check Extended Totals" appears even though both values = 0.
 
Upvote 0
See if this works for you. I've based it on the value in C228.
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.EnableEvents = True
    Dim wsMaster As Worksheet, wsSUMMARY As Worksheet, extHeaderCell As Range, masterTotal As Double
    Set wsMaster = ThisWorkbook.Sheets("Master")
    Set wsSUMMARY = ThisWorkbook.Sheets("SUMMARY")
    Set extHeaderCell = wsMaster.Rows(1).Find("Extended", LookIn:=xlFormulas, lookat:=xlWhole)
    masterTotal = extHeaderCell.Offset(, 1).Value
    If masterTotal = 0 And wsSUMMARY.Range("C228") = 0 Then
        MsgBox "Extended Totals Match"
    Else
        If wsSUMMARY.Range("C228") = masterTotal Then
            MsgBox "Extended Totals Match"
        Else
            MsgBox "Please check Extended Totals match!"
            Cancel = True
        End If
    End If
End Sub
 
Upvote 0
See if this works for you. I've based it on the value in C228.
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Application.EnableEvents = True
    Dim wsMaster As Worksheet, wsSUMMARY As Worksheet, extHeaderCell As Range, masterTotal As Double
    Set wsMaster = ThisWorkbook.Sheets("Master")
    Set wsSUMMARY = ThisWorkbook.Sheets("SUMMARY")
    Set extHeaderCell = wsMaster.Rows(1).Find("Extended", LookIn:=xlFormulas, lookat:=xlWhole)
    masterTotal = extHeaderCell.Offset(, 1).Value
    If masterTotal = 0 And wsSUMMARY.Range("C228") = 0 Then
        MsgBox "Extended Totals Match"
    Else
        If wsSUMMARY.Range("C228") = masterTotal Then
            MsgBox "Extended Totals Match"
        Else
            MsgBox "Please check Extended Totals match!"
            Cancel = True
        End If
    End If
End Sub
Thank you! That seems to work perfectly. Sometimes there is an inserted column before 'C' on SUMMARY page for sub-section values, pushing the grand total to d228. Would that be as simple as changinging the range provided from c228 to c228:d228?
 
Upvote 0
Replace
VBA Code:
If wsSUMMARY.Range("C228") = masterTotal Then
with
VBA Code:
If wsSUMMARY.Range("C228") = masterTotal Or wsSUMMARY.Range("D228") = masterTotal Then
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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