Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

How to track highest stock price across sessions

0

In a sheet tracking stock prices and transactions, I want to have a column that shows the highest price a stock has risen to.  In other words, I need to make this concept work ( =IF(A1 > B1, A1,B1 ) where the A1 would be current stock price, and B1 is a saved (persistent) highest value for A1.  I keep running into 'circular reference' errors. 

I'm sure someone has done this before.

I rather suspected I'd need code to solve this, but I have never coded for Excel.  So an example of the code I'd need, and where/how to attach it to the worksheet would be most helpful.  Something that would function like the formula above, but without the circular references that I keep encountering. Then I could copy it to all the stock positions I'm tracking where each row is a different stock.

Answer
Discuss

Discussion

As Variatus has said, you can't do this automatically with worksheet functions but it is possible via code. Note that you will keep running into 'circular reference' errors  since Excel cannot evaluate a cell formula whose value depends on that cell's value, directly or indirectly. 
John_Ru (rep: 6142) May 11, '21 at 1:32 am
I encounter a problem when I try to assign a value to another cell that will persist. I do not know how to do that.  I would really appreciate a sample of a vba macro or subroutine that can be part of the sheet and accomplishes what I need to do.
l am updating my sheet manually at EoD so the system Variatus offers is not applicable in my case.
EZriderz May 11, '21 at 5:57 pm
Please attach a sanitized copy of your workbook to your question. You can do that in Edit mode. While all data may be removed, it's essential that you specify which cell or cells you intend to update and in which related cell or cells you wish to see the maximum or minimum. 
While modifying your question, please also specify the period. Do you want the daily max, monthly max, highest ever or restart every year or hour?
Variatus (rep: 4889) May 11, '21 at 8:35 pm
Variatus, thanks again for the help. Your question suggests a more complex solution than I seek. I just want to have a cell next to the current close price for a stock position on my sheet. That cell would hold the highest price that stock has been recorded on the sheet. I just don't know how to capture the highest price and save it for future comparisons without encountering the circular reference error. 
EZriderz May 12, '21 at 4:43 pm
It's not possible to do what you want with worksheet functions. Code is unavoidable. 
Variatus (rep: 4889) May 12, '21 at 8:48 pm
Add to Discussion

Answers

0

 You need code for this. However, what kind of code you need depends upon how you udate yoiur stock quotes. Here is a system where the update is done automatically.  If you update the quotes manually a much simpler method can be used.

This ethod is demonstrated in the attached workbook. This is the code it contains. Observe that it must be in the code sheet belonging to the worksheet on which you want the action. That is a module created by Excel, not one you inserted yourself. In the attached workbook it's the module of the worksheet TXL_5418.

Private Sub Worksheet_Change(ByVal Target As Range)
    ' 240

    Const TriggerClm    As String = "A"     ' change to suit
    Const RecordClm     As String = "C"     ' change to suit

    Dim TriggerRng      As Range            ' defining rows in TriggerClm
    Dim Trigger         As Variant          ' the changed value

    ' react to changes from row 2 to the row below the last used row
    Set TriggerRng = Range(Cells(2, TriggerClm), _
                           Cells(Rows.Count, TriggerClm).End(xlUp).Offset(1))
    With Target
        ' don't react if several cells were changed simultaneously
        If .Cells.CountLarge = 1 Then
            Trigger = .Value
            With Cells(.Row, RecordClm)
                If Val(Trigger) > Val(.Value) Then
                    ' prevent the change from calling this procedure
                    Application.EnableEvents = False
                    .Value = Trigger
                    Application.EnableEvents = True
                End If
            End With
        End If
    End With
End Sub

Please take the time to read the comments I inserted in the code.

Discuss

Discussion

Thanks, Variatus, but l am updating my sheet manually at EoD.
EZriderz May 11, '21 at 5:57 pm
I've updated my question above to clarify.  I read that more people read the original questions than reading the discussions.
EZriderz May 15, '21 at 5:01 pm
Yes. That's true. Thank you for your consideration. I'll look into your revised question.
Variatus (rep: 4889) May 15, '21 at 7:49 pm
Thank you, Variatus, but that is a bit beyond me.  I'm but an egg when it comes to programming for Excel.
1) the code page "240 TXL 210516 Change Event.xlsm" is for a later version of Excel than my 2003, and when I ran it through the file converter for older versions, it did open, but only with 3 rows of 1,2,3,4 in col A2:A5, and a 3 in C3.  No code at all is visible
2) This is a much more ambitions block of code than I can understand. I'm looking for 'if A1 > B1 then save A1, else store B1 in A1'. I do not know where to even store the VBA code snippet in the workbook. And I don't know how to call the function/sub from the worksheet.  
EZriderz May 19, '21 at 3:18 pm
I believe my code should run in Excel 2003. Please open my workbook, change any of the numbers in column A and observe column C. That's where the highest number ever entered in the same row of column A will / should be recorded. If that is so, you can transfer my code to your own workbook and expect it to work.
Press Alt+F11 to open the VB editor. You should see the Project Explorer pane on the left. If not, press Ctl+R or select it from the View menu. In this pane you will see a list of the worksheets and one entry called ThisWorkbook. These are the code modules in the workbook. In workbook "240 TXL" double-click on "Sheet1 (TXL_5416)". That's where you will find the code.
In your own workbook double-click on the name of the worksheet on which you want the action. You can copy the code from my workbook or from the above post and paste it there. That will install the action on your workbook.
You don't need to call the procedure. It calls itself. Or, rather, it is triggered by a change in the TriggerClm. In the code you will see that it's column "A". You can change that in the code. You can also change column "C" (the column where the record is kept). Once you have set it up you can forget it.
Regretfully: There is no formula that can do what you wish to do. If you want the action, this is the only way to get it.
Variatus (rep: 4889) May 19, '21 at 9:06 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login