Display message if column total exceeds using VBA in Microsoft Excel

In this article, we will use change event of worksheets to display a warning message when the column total exceeds total value.

Raw data consists of salt composition of a medicine. It comprises of salt name and it's weight per 10 grams.

ArrowRaw

Total of all the salt ingredients is equal to 10 grams. When we change the weight of ethane salt from 0.75 to 1, a warning message will pop-up.

ArrowOutput

Logic explanation

In this article, we have used change event of worksheet to generate warning message. Change event checks the total of 2nd column, which should not exceed 10.

Code explanation

If Target.Column = 2 Then

The above code is used to restrict change event to trigger only when value in the 2nd column is changed.

WorksheetFunction.Sum(Columns(2))

The above code is used to get sum of all the values in column 2.

 

Please follow below for the code

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

'Checking whether value is changed in second column
If Target.Column = 2 Then
    
    'Checking whether sum of values in second column is greater than 10
    If WorksheetFunction.Sum(Columns(2)) > 10 Then
        MsgBox "Caution, Total has exceeded 10g"
    End If

End If

End Sub

 

If you liked this blog, share it with your friends on Facebook. Also, you can follow us on Twitter and Facebook.

We would love to hear from you, do let us know how we can improve our work and make it better for you. Write to us at info@exceltip.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.