Message Box popup if cell is negative

jrisebo

Active Member
Joined
Mar 15, 2011
Messages
325
Office Version
  1. 365
Platform
  1. Windows
Im trying to get some code to work for a range of cells being negative. Lets say A1 to A5 are the cells to check, I want a message box to pop up saying "Hey, one of your values is negative" The values wont be data entry, but a cell that is calculated based on some other entry.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
VBA Code:
Sub FFF()
  If Application.Min([A1:A5]) < 0 Then MsgBox "One of the values is negative", vbExclamation
End Sub
 
Upvote 0
Thanks this worked.

Anyway to only get it to show up once per opening of the file, i.e. "disable until nextime I open file" checkbox?
 
Upvote 0
Place in ThisWorkbook code module. - Copied from @Sektor
VBA Code:
Private Sub Workbook_Open()
 If Application.Min([A1:A5]) < 0 Then MsgBox "One of the values is negative", vbExclamation
End Sub
 
Upvote 0
One more thing on this, I dont want this code to run until a value is entered into a specific cell, i.e. C1 cell. So the user can do what they want, and then when they type in the C1 cell, the code becomes active.
 
Upvote 0
ok - you're describing an event in a defined Sheet. So no Workbook_Open event of course. Selection_Change ist better. And you have to check two conditions: cell.value <>"" and cell.value<0. Try this in your sheet:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Integer
'If Target.Row = 10 And Target.Column < 6 Then    'that's A10:E10
If Target.Address = "$C$10" Then
    For i = 1 To 5
        If Cells(10, i) <> "" And Cells(10, i) < 0 Then
            MsgBox "Alert in cells(10," & i & ")"
        End If
    Next i
End If
End Sub
Good luck!

Newbie sen.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
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