Alert when dynamic value is greater a number - excel

I'm looking for a way for excel to alert me when a dynamic value (market feed data) is out of line by x%. The alert needs to appear in front of all my windows with a msg box "CHECK VALUES".
Is this possible to do and has anyone have an example of code for this?
More specifically
Minimum difference = 0.5
CELL A = 10
CELL B = 11
Difference = 1
ALERT user "Difference > 0.5"
Thank you in advance.
Thank you. This works perfect. Now I understand how it works, I'm hoping for another solution similar to the above. Both cells in A1 and B1 are constantly changing(variables). Cell C1 is the =ABS(B1-A1). What I need now is a code to alert me when Cell C1 is greater than 0.5.

You can use worksheet_change event. Try below code.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo err_rout
Application.EnableEvents = False
If Target.Address = "$A$1" And Target.CountLarge = 1 Then
If (Target.Value - Target.Offset(0, 1)) > 0.5 Then
MsgBox "CHECK VALUES"
End If
ElseIf Target.Address = "$B$1" And Target.CountLarge = 1 Then
If (Target.Value - Target.Offset(0, -1)) > 0.5 Then
MsgBox "CHECK VALUES"
End If
End If
err_rout:
Application.EnableEvents = True
End Sub

Related

Function that changes the cell color

I am creating a function where if a cell has Yellow color and if I enter 0 in that cell, then it pops up a message box Red then colors the cell Red. And then if I enter 1 it reverts the color to Yellow.
Here's my code:
Function ColorChange(range)
If range.Interior.ColorIndex = 6 And range.Value < 1 Then
MsgBox "Project Delay!", vbCritical, "Attention required!"
range.Interior.ColorIndex = 3
Else
If range.Interior.ColorIndex = 3 And range.Value > 0 Then
range.Interior.ColorIndex = 6
End If
End If
End Function
1) It is not necessary to use a function. In VBA you can program classically (and run the macro manually) or you can program event. It means that a macro will be running in case of a particular event (like a mouse-click or a modification in the sheet). That is why Siddarth Rout spoke about Worksheet_change, it is an event. In this case, define your Sub in this way and the following code will be executed in case of Worksheet modification.
2) You have to define a Private Sub for the specific worksheet. Private Sub Worksheet_change() and you put in argument ByVal Target As range. It means that you will receive information about the range that have been changed (Target).
3) After you will use Target.Address to define the range used by your code.
4) Your 2 intricate if statements are not necessary. It is better to use an if statement and an else if statement.
5) The following code is OK for me:
Private Sub Worksheet_change(ByVal Target As range)
If range(Target.Address).Interior.ColorIndex = 6 And range(Target.Address).Value < 1 Then
MsgBox "Project Delay!", vbCritical, "Attention required!"
range(Target.Address).Interior.ColorIndex = 3
ElseIf range(Target.Address).Interior.ColorIndex = 3 And range(Target.Address).Value > 0 Then
range(Target.Address).Interior.ColorIndex = 6
End If
End Sub

Excel VBA formatting and variable output not always working

If a specific value will entered in row "A", a specific price should be inserted into row "D" and after that the entered price should be displayed in a messagebox.
The first part was just an easy setup, but with the msgbox I have actually some issues.
Maybe because of the procedure of the code?! The price is just in this moment inside the cell and my code is already trying to get this in the moment empty cell?! - not sure.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Handler
Dim price As String
If Target.Column = 1 And Target.Value = "XY01" Then
Application.EnableEvents = False
Target.Offset(0, 3) = Format(0.7, "currency")
Application.EnableEvents = True
price = ActiveCell.Offset(0, 3).Value
MsgBox "The price is now " & price
End If
Handler:
End Sub
The really strange thing is that inside the first row it will be displayed as excepted:
Just in every other row it will be displayed like this (it's just empty):
My 2nd question is that I have formatted the value as "currency", but I'm anyway get this error message (in English like that the cell is formatted as text). Also by formatting the cell by the excel tools the error message will not disappear.
Any Idea to fix this?
Thank you guys.
==============
EDIT
I have updated my code to following, so I was able to solve my 2nd question regarding the note that my value is just a text.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Handler
Dim price As String
If Target.Column = 1 And Target.Value = "XY01" Then
Application.EnableEvents = False
Target.Offset(0, 3).Value = 0.7
Target.Offset(0, 3).NumberFormat = "currency"
Application.EnableEvents = True
price = Target.Offset(0, 3).Text
MsgBox "The price is now " & price
End If
Handler:
End Sub
I don't know why, but now will not be any msgbox displayed?!
Also the price will now only once inserted, if I type in the code again in another cell (a cell down) the code seems not be running again?!
I need to reopen excel to bring it up to work again.
The Format function always returns a string/text.
So here: Target.Offset(0, 3) = Format(0.7, "currency") you don't write a numeric value but a text.
Instead write the value and set the number format of the cell:
Target.Offset(0, 3).Value = 0.7
Target.Offset(0, 3).NumberFormat = "#,##0.00 $"
Then you can read the cell's .Text (instead of .Value) to get it formatted as shown in cell:
Dim price As Sting
price = Target.Offset(0, 3).Text
MsgBox "The price is now " & price
or read the cell's Value and format it whatever you like:
Dim price As Double
price = Target.Offset(0, 3).Value
MsgBox "The price is now " & Format(price, "#,##0.00 $")
Can you try to change this line:
Dim price As Double
Good Luck

Excel Macro VBA: Double click to tick issue

how can I make the double click works ONLY when column 1,2,3 and 4 have values? I don't know where I should insert the code.. it's something like if column 1,2,3 and 4 have values then doubleclick.enable = TRUE else doubleclick.enable= FALSE..
Kindly need advice. My code is as follows:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Select Case Target.Column
Case 6, 13
If Not Intersect(Target, Range("F2:F13, M2:M13")) Is Nothing Then Cancel = True
Target.Font.Name = "Times New Roman"
If Target = "" Then
Target = ChrW(&H2713)
Else
MsgBox "You cannot modify the cell."
End If
End Select
End Sub
I am asking just to clarify the issue.
You only want to call doubleclick procedure if any cell in column 1 to 4 have value
Or you want to check if corresponding row has value?
for option 1, you may use
If Application.CountA(Range("A1:D" & Rows.Count)) > 0 Then
'Your Code
End If
for second option
If Application.CountA(Range("A" & target.Row & ":D" & target.Row)) > 0 Then
'Your Code
End If
I tested above codes but in case of any mistake, we can modify them

How to Hide and Unhine rows using dollar figures using VBA

My apologies if my question is too basic. I am trying to achieve the following results
If Cell E11 value is less than 25,000, then hide Rows 14 & 15.
If Cell E11 value is between 25k-50k, then hide only row 15 only.
If Cell E11 value is between 50k-75k, then unhide both rows.
And it is possible to make the calculation automated?
So far I found the following code, which of course isn't helping with my situation.
Sub PG1()
If Range("E11").Value = "Pass" Then
Rows("14:14").EntireRow.Hidden = True
ElseIf Range("E11").Value = "Fail" Then
Rows("14:14").EntireRow.Hidden = False
End If
End Sub
A minor amount of trial and error based on nothing else but the code you posted gave me this code, which should get you started. It completes two of the three requirements (using different cells and rows), but it works. If it's still not something you can use to complete your task, you should probably hire someone to do this for you.
Sub ShowOrHide()
If ActiveSheet.Range("A1").Value < 25000 Then
ActiveSheet.Rows("2:3").EntireRow.Hidden = True
ElseIf ActiveSheet.Range("A1").Value >= 50000 Then
ActiveSheet.Rows("2:3").EntireRow.Hidden = False
End If
End Sub
Here you go.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$11" Then
If Target.Value < 25000 Then Rows("13:15").EntireRow.Hidden = True
If Target.Value > 25000 Then Rows("13:14").EntireRow.Hidden = False
If Target.Value > 50000 Then Rows("13:15").EntireRow.Hidden = False
If Target.Value > 75000 Then Rows("13:15").EntireRow.Hidden = True
End If
End Sub

Worksheet.Change Event in Excel in real time

Take as example the following code which should state whether the content of Cell A3 is greater or smaller than 0.25, where in Cell A3 we have a formula like RAND() whoch generates random numbers between 0 and 1.
Private Sub Worksheet_Change(ByVal Target As Range)
Calculate
If Range("A3") > 0.25 Then
Range("B3") = "greater than 0.25"
Else: Range("B3") = "smaller than 0.25"
End If
End Sub
How to make this Event conditions to be verified in continuous time?
What about using timer? this function calls each 1 second
Sub Test()
Calculate
If Range("A3") > 0.25 Then
Range("B3") = "greater than 0.25"
Else
Range("B3") = "smaller than 0.25"
End If
Application.OnTime Now + TimeSerial(0, 0, 1), "Test"
End Sub
I think there is no option for that. Have been looking for it myself. There is no change event for calculated cells. The only option is to check dependencies of a calculated field but in this case there are none.

Resources