Message box linked to cell value, different for positive, negative or same value

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Hi

Sheet 'Training Log' E5 contains the formula "=C5-D5"

When the workbook opens I need a message box to run with a message depending on one of these 3 conditions:

If C5-D5 is negative:
"You have now run (Value in E5) & "miles" less than this time last year"

If C5-D5 is the same:
"You have run the same number of miles as of this time last year"

If C5-D5 is positive:
"You have now run (Value in E5) & "miles more than this time last year"

Many thanks!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
How about
VBA Code:
Private Sub Workbook_Open()
   Select Case Sheets("Training Log").Range("E5")
      Case Is < 0: MsgBox "You have now run " & .Value & " miles less than this time last year"
      Case 0: MsgBox "You have run the same number of miles as of this time last year"
      Case Else: MsgBox "You have now run " & .Value & " miles more than this time last year"
   End Select
End Sub
 
Upvote 0
Many thanks Fluff!

I got an error message with
VBA Code:
MsgBox ...& .Value

"Invalid or unqualified reference"

I amended it to
VBA Code:
Select Case Sheets("Training Log").Range("E5")
    Case Is < 0: MsgBox "You have now run " & Range("E5").Value & " miles less than this time last year"
    Case 0: MsgBox "You have run the same number of miles as of this time last year"
    Case Else: MsgBox "You have now run " & Range("E5").Value & " miles more than this time last year"
End Select
And it works, thank you!

I just need the figure rounding up to the nearest whole number.

I've tried this
VBA Code:
Select Case Sheets("Training Log").Range("E5")
    Case Is < 0: MsgBox "You have now run " & CLng(Range("E5").Value) & " miles less than this time last year"
    Case 0: MsgBox "You have run the same number of miles as of this time last year"
    Case Else: MsgBox "You have now run " & CLng(Range("E5").Value) & " miles more than this time last year"
End Select
But it's prefixed by a "-", which I don't want, as the text explains that. I just need the value without a positive or negative prefix?

Thanks again!
 
Last edited:
Upvote 0
VBA Code:
Private Sub Workbook_Open()
    With Sheets("Training Log").Range("E5")
        Select Case .Value
            Case Is < 0: MsgBox "You have now run " & -.Value & " fewer miles than this time last year"
            Case 0: MsgBox "You have run the same number of miles as of this time last year"
            Case Else: MsgBox "You have now run " & .Value & " miles more than this time last year"
        End Select
    End With
End Sub
 
Upvote 0
Missed the with statement, try
VBA Code:
Private Sub Workbook_Open()
   With Sheets("Training Log").Range("E5")
      Select Case .Value
         Case Is < 0: MsgBox "You have now run " & CLng(Abs(.Value)) & " miles less than this time last year"
         Case 0: MsgBox "You have run the same number of miles as of this time last year"
         Case Else: MsgBox "You have now run " & CLng(.Value) & " miles more than this time last year"
      End Select
   End With
End Sub
 
Upvote 0
Solution
Perfect, Fluff!

Thank you so much!

(thanks also JGordon11 for your input)
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,939
Members
449,094
Latest member
teemeren

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