VBA code: msg box if sum of cells do not equal cell "A1"

Katyjean1

New Member
Joined
Apr 30, 2019
Messages
12
I need a code to stop the other codes from running if the sum of cells O16:O26 do not equal cell O10. Also need the msgbox warning. This is what I've found so far. It's not working for me. Can anyone help? :)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    If Application.WorksheetFunction.Sum(Range("O16:O26")) <> (Range("O10")) Then
        MsgBox "Distribution amt does not match total amount"
    End If
    
End Sub

I currently have a command button set to run 2 other codes. Should this stop code be written onto Sheet1 in VBA? Or can it be a separate module and added to the top of the button click list?

Thanks for all the help today! ;)
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Just place this code at the top of the Command Button code that is calling your other codes:
Code:
    If Application.WorksheetFunction.Sum(Range("O16:O26")) <> (Range("O10")) Then
        MsgBox "Distribution amt does not match total amount"
        Exit Sub
    End If
So, if the condition is not met, it will return the Message Box and exit the sub procedure before it calls the other two macros.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
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