PDA

View Full Version : VBA Macro code to get an alert message when a condition is met



laxmikant_74
05-22-2017, 08:45 AM
Hi Guys
I am new to VBA.Attached is a file for which I need the code.Now there are 4 columns.The first column consists of names of the companies.The second column is the market price of the respective companies that gets updated on real time basis.The third column and the fourth column consist of thhe calculated buy price and sell price of that company.Now if the market price is equal to the buy price of that company then I should get a pop up message saying " Buy Co. XYZ" or if the market price is equal to the sell price of that company then I should get a pop up message saying"Sell Co. XYZ".
Kindly help.Please remenber that the price in column 2 gets updated every second on real time basis
Thanks Laxmikant

Logit
05-22-2017, 11:35 AM
.
.
Paste this macro into the Sheet Level module for the affected sheet:



Option Explicit


Private Sub Worksheet_Change(ByVal Target As Range)
If Target Is Nothing Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
If Target.Column <> 2 Then Exit Sub

'BUY routine begins here
If Cells(Target.Row, 2).Value = Cells(Target.Row, 3).Value Then
'Cells(Target.Row, 5).Value = "BUY - " & Cells(Target.Row, 1).Value
Cells(Target.Row, 1).Interior.Color = vbYellow
MsgBox "BUY ! " & Cells(Target.Row, 1).Value
'SELL routine begins here
ElseIf Cells(Target.Row, 2).Value = Cells(Target.Row, 4).Value Then
'Cells(Target.Row, 5).Value = "SELL - " & Cells(Target.Row, 1).Value
Cells(Target.Row, 1).Interior.Color = vbYellow
MsgBox "SELL ! " & Cells(Target.Row, 1).Value
Else

Cells(Target.Row, 1).Interior.Color = xlNone
End If


End Sub