VBA Code to Enter a String Text when the trigger cell's value meets certain conditions

JohnGow383

Board Regular
Joined
Jul 6, 2021
Messages
141
Office Version
  1. 2013
Platform
  1. Windows
Hi all,

I would like code to automatically generate text in a cell when another cell's value meets the preset conditions. I may also want to add a message box too.
So for example:
Trigger Cell = H6 if Value of H6<0 or H6>200 to populate merged cells "H13:K14" with text "Warning! Please Check the LSFO Meter Readings"
To also add a message box to the same effect (I can remove later if this is too annoying).
Once the error condition is no longer met, i.e. the value of H6 is between 0 to 200, for the populated merged cell "H13:K14" to have it's contents cleared.

I also have other trigger cells, where the warning message would be slightly different, but would it be just a case of repeating a small section of the code within the same macro or would I have to make multiple macros for each trigger cell?

Thanks
 
Try:

1) Its good practise to put:
"Option Explicit" on top of code
try it.

2) to replace:
"If Not Application.Intersect(triggercells, Range(Target.Address)) Is Nothing Then"
with
"If Not Intersect(target, triggercells) Is Nothing Then"

to see if it in good order?
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I'm just thinking, is it not working because the H6 contains a formula? The value of H6 though is a numerical number so it shouldn't really matter.
 
Upvote 0
will try now thanks
Try:

1) Its good practise to put:
"Option Explicit" on top of code
try it.

2) to replace:
"If Not Application.Intersect(triggercells, Range(Target.Address)) Is Nothing Then"
with
"If Not Intersect(target, triggercells) Is Nothing Then"

to see if it in good order?
 
Upvote 0
Or, try to delete first part (your initial code), just use only second (my code), then try to change value in H6

???
 
Upvote 0
I'm just thinking, is it not working because the H6 contains a formula? The value of H6 though is a numerical number so it shouldn't really matter.
That why H6 was not triggerred.
Change even only work for manual input.
For change from formula, it should be fired with worksheet_calculate even. (calculate does not fire single range, it fire change from every cells in whole sheet)
 
Upvote 0
No, that hasn't helped. The
"If Not Intersect(target, triggercells) Is Nothing Then" line still works with the other parts of the code though.
It is not being triggered for some reason


Try:

1) Its good practise to put:
"Option Explicit" on top of code
try it.

2) to replace:
"If Not Application.Intersect(triggercells, Range(Target.Address)) Is Nothing Then"
with
"If Not Intersect(target, triggercells) Is Nothing Then"

to see if it in good order?
 
Upvote 0
Sorry, bit confused
That why H6 was not triggerred.
Change even only work for manual input.
For change from formula, it should be fired with worksheet_calculate even. (calculate does not fire single range, it fire change from every cells in whole sheet)

Like something like this
VBA Code:
Private Sub Worksheet_Calculate()
Dim ce1 As Range, msg1 As String
Set ce1 = Range("H6"): msg1 = "Warning! Please Check the LSFO Meter Readings"
    With Range("H13:K14")
        If Not Intersect(Target, ce1) Is Nothing Then
            Select Case ce1.Value
                Case 0 To 200
                    .ClearContents
                Case Else
                    .Value = msg1
                    MsgBox msg1
            End Select
        End If
    End With
End Sub
 
Upvote 0

Attachments

  • Capture.JPG
    Capture.JPG
    85.5 KB · Views: 6
Upvote 0
Opp, sorry, I forgot to tell you that calculate even does not require target. Try to remove 2 lines of "If ...End if"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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