Show message when the value in a column is negative

Simona

New Member
Joined
Aug 9, 2010
Messages
13
Dear All,

I've been trying to create an automatic check that will tell me when any value in the column CB turns negative.
I found the following code on the internet but I'm not able to make it work.
Could someone please help me!

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    With Target
        If .Count > 1 Then Exit Sub
            If Not Range("$CB$8:$CB$500").Cells Is Nothing Then
                Application.EnableEvents = False
                If Range("$CB$8:$CB$500").Value < 0 Then
                   MsgBox "Negative Value. Plese correct the amount before continuing."
                End If
                Application.EnableEvents = True
            End If
        End With
    End Sub
Thanks,
Simona
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim MyRange As Range, c As Range
Set MyRange = Intersect(Target, Range("CB:CB"))
If MyRange Is Nothing Then Exit Sub
For Each c In MyRange
    If c.Value < 0 Then 
        MsgBox "Negative value was entered in " & c.Address & VbNewLine & "Please correct the amount before continuing"
        Exit For
    End If
Next c
End Sub

Hope that helps.
 
Upvote 0
Hi Jonmo1,

thank you for your prompt response!!!
Your code works fine if I enter manually a negative amount in the column CB but it doesn't work if in the column CB I have formulas instead.
I have one amount in the column A and then a few deductions in the other columns (between B and CA).
As the total of deductions can't be higher than the original value in column A I want to create an automatic check.
Is it possible to create a code that checks when the formula returns a negative value?

Thank you again!
 
Upvote 0
Dear Jonmo1,

after some researches over the internet I found out that the Worksheet_Change doesn't work with formulas.
I used instead the Worksheet_Calculate changing slightly your code and now everything is working fine.

Here we go:

Code:
Private Sub Worksheet_Calculate()
    Dim MyRange As Range, c As Range
    Set MyRange = Range("CB:CB")
    If MyRange Is Nothing Then Exit Sub
    For Each c In MyRange
    If c.Value < 0 Then
        MsgBox "Negative value was entered in " & c.Address & vbNewLine & "Please correct the amount before continuing"
        Exit For
    End If
Next c
End Sub
Thank you for your time,
Simona
 
Upvote 0
Glad you got it sorted out...

Another approach when the cells are formulas is...

Instead of using the Calculate event,
Look at what cells your formula refers to, and use the Change event to monitor THOSE cells instead.


I prefer this approach because the Calculate Event triggers every time ANYTHING on the sheet changes. Regardless of which formula updated.

This is also true of the change event, however..
With the Change event, you can restrict your code to run on only if specific cells changed
by using Intersect(target, range("...")).

There is no Target in the Calculate event.
So there is no way to know WHICH formula changed.
So you can't restrict your code to run based on which formula changed...

So if the value of another formula completely unrelated to the one you're monitoring changes, your code still runs needlessly.


So for example,
If your formulas in column CB are
=SUM(A1:B1) 'simple example I know..

Then you would do this..

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim MyRange As Range, c As Range
    Set MyRange = Range("CB:CB")
 
    'This makes the code run only if the cells your formula refer to are changed.
    If Intersect(Target, Range("A:B")) Is Nothing Then Exit Sub
 
    For Each c In MyRange
    If c.Value < 0 Then
        MsgBox "Negative value was entered in " & c.Address & vbNewLine & "Please correct the amount before continuing"
        Exit For
    End If
Next c
End Sub


Hope that helps.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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