Consulting

Results 1 to 2 of 2

Thread: VBA Macro code to get an alert message when a condition is met

  1. #1

    VBA Macro code to get an alert message when a condition is met

    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
    Attached Files Attached Files

  2. #2
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    .
    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
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •