VBA Pop Up Message Using Moving Cell Values

h3artablaze

New Member
Joined
Aug 28, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,
Just to preface, I'm very new to VBA so not sure if starting in the right place here. I'm trying to get a VBA error message to pop up depending on value in cell F5. If F5 = Q5 I'd like "Take Profit Level has been reached" to appear, whereas if F5=R5 I'd like "Stop/Loss Level has been reached" to appear. F5 is a changing value dependent on a function.

Private Sub Calculate(ByVal Target As Range)
If Target.Address = "$F$5" Then
If Target.Value = "$Q$5" Then MsgBox "Take Profit Level has been reached"
If Target.Value = "$R$5" Then MsgBox "Stop/Loss Level has been reached"
End If
End Sub

I went with the above after some light research but clearly is wrong...

Many thanks in advance
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi,
Just to preface, I'm very new to VBA so not sure if starting in the right place here. I'm trying to get a VBA error message to pop up depending on value in cell F5. If F5 = Q5 I'd like "Take Profit Level has been reached" to appear, whereas if F5=R5 I'd like "Stop/Loss Level has been reached" to appear. F5 is a changing value dependent on a function.

Private Sub Calculate(ByVal Target As Range)
If Target.Address = "$F$5" Then
If Target.Value = "$Q$5" Then MsgBox "Take Profit Level has been reached"
If Target.Value = "$R$5" Then MsgBox "Stop/Loss Level has been reached"
End If
End Sub

I went with the above after some light research but clearly is wrong...

Many thanks in advance
Give this a try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$5" Then
    If Target.Value = Range("Q5").Value Then MsgBox "Take Profit Level has been reached"
    If Target.Value = Range("R5").Value Then MsgBox "Stop/Loss Level has been reached"
End If
End Sub
 
Upvote 0
Welcome to the Board!

F5 is a changing value dependent on a function.
dreid1011,
Because of that comment above, I do not think your code will work. Worksheet_Change only runs when hard-coded values are updated, not changes that happen due to formulas in that cell.

h3artablaze,
This is not valid:
Rich (BB code):
Private Sub Calculate(ByVal Target As Range)
If Target.Address = "$F$5" Then

The proper call to the "Calculate" event it:
VBA Code:
Private Sub Worksheet_Calculate()
Note how the name is "Worksheet_Calculate", and there is no parameter.
Event procedures are VERY strict. You cannot change the name or the parameters that they allow.

The issue with the Calculate event procedures is that they cannot identify which cell's value was updated/re-calculated. All that it can tell you is that some value somewhere on the sheet was re-calculated. So I don't think it is going to be helpful to you here.

What exactly is the formula in cell F5?
If it is a function of other cells whose values are hard-coded, then we can use a "Worksheet_Change" event procedure to watch for those values to change, and then do your check.
 
Upvote 0
Welcome to the Board!


dreid1011,
Because of that comment above, I do not think your code will work. Worksheet_Change only runs when hard-coded values are updated, not changes that happen due to formulas in that cell.
Ah, fair enough. Thank you.
 
Upvote 0
Welcome to the Board!


dreid1011,
Because of that comment above, I do not think your code will work. Worksheet_Change only runs when hard-coded values are updated, not changes that happen due to formulas in that cell.

h3artablaze,
This is not valid:
Rich (BB code):
Private Sub Calculate(ByVal Target As Range)
If Target.Address = "$F$5" Then

The proper call to the "Calculate" event it:
VBA Code:
Private Sub Worksheet_Calculate()
Note how the name is "Worksheet_Calculate", and there is no parameter.
Event procedures are VERY strict. You cannot change the name or the parameters that they allow.

The issue with the Calculate event procedures is that they cannot identify which cell's value was updated/re-calculated. All that it can tell you is that some value somewhere on the sheet was re-calculated. So I don't think it is going to be helpful to you here.

What exactly is the formula in cell F5?
If it is a function of other cells whose values are hard-coded, then we can use a "Worksheet_Change" event procedure to watch for those values to change, and then do your check.
Hi,

Thanks! The formula in Cell F5 is essentially an interest rate interpolation, (it is not something I have designed so unfortunately cannot elaborate much further)... referencing both hard codes and formulas in the sheet

Cheers for your help both
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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