Forum Discussion

dhanu1660's avatar
dhanu1660
Copper Contributor
Jun 01, 2021
Solved

Highest Lowest price from running price

Hi friends
this is stock market related issue.
L9 cell has a price it shows profit and loss,it changes every second.
L9 Profit
M10 max profit
M12 max loss
example
1. if L9 has 100 profit then M10=100
. if L9 updated 110 then M10=110
. if L9 updated to 90 then M10=110
M10 should be update only for highest values not for lowest values.
  • dhanu1660 

    Start by entering -1000000 in M10 and 1000000 in M12. These are initial values that will be overwritten.

    Enter the formula =NOW() in another cell (it doesn't matter where).

    Right-click the sheet tab.

    Select 'View Code' from the context menu.

    Copy the following code into the worksheet module:

    Private Sub Worksheet_Calculate()
        Application.EnableEvents = False
        Range("M10").Value = Application.Max(Range("L9,M10"))
        Range("M12").Value = Application.Min(Range("L9,M12"))
        Application.EnableEvents = True
    End Sub

    Switch back to Excel.

    Save the workbook as a macro-enabled workbook.

    Make sure that you allow macros when you open it.

29 Replies

  • tusharm10's avatar
    tusharm10
    Brass Contributor

    Another way to do this without code is to enable iterative calculations.

    File | Options | Formulas tab | Calculation Options section | check the Enable iterative calculation checkbox and also change Maximum Iterations to 1.

     

    Then in M10, enter the formula

    =MAX(L9, M10)
    

     

    dhanu1660 

  • dhanu1660 

    Start by entering -1000000 in M10 and 1000000 in M12. These are initial values that will be overwritten.

    Enter the formula =NOW() in another cell (it doesn't matter where).

    Right-click the sheet tab.

    Select 'View Code' from the context menu.

    Copy the following code into the worksheet module:

    Private Sub Worksheet_Calculate()
        Application.EnableEvents = False
        Range("M10").Value = Application.Max(Range("L9,M10"))
        Range("M12").Value = Application.Min(Range("L9,M12"))
        Application.EnableEvents = True
    End Sub

    Switch back to Excel.

    Save the workbook as a macro-enabled workbook.

    Make sure that you allow macros when you open it.

Resources