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

Tracking high (low) values

0

Whenever I update my spreadsheet there is a particular cell (x) whose numeric value may be changed. In another cell (y) I need to track the highest value that cell x has ever attained.

In essence I need to replace the value in cell y with the value in cell x if the value in cell x ever exceeds the value in cell y.

Using the IF function in cell y to do this gives a circular reference error.

Thanks

Answer
Discuss

Answers

0
Selected Answer

Hi Gest and welcome to the Forum

You'd need to use VBA (Excel's built-in programming language) to do that, a static IF function won't work.

Suggest you look at a recent answer by top contributor Variatus, here: How to capture additional highest and lowest value for additional Columns

If you just have one value to track, then a simple Worksheet_Change event macro will do it in 2 lines (as follows, commented for your guidance):

Private Sub Worksheet_Change(ByVal Target As Range)

' see if moving cell was changed
If Intersect(Target, Range("B1")) Is Nothing Then Exit Sub

' if more than high cell, make high cell the new value
If Range("B1").Value > Range("B3").Value Then Range("B3").Value = Range("B1").Value

End Sub
Change the cell shaded yellow (B1) and the macro will update B3 in B1 exceeds its current value. You'll need to change the B1 and B3 cell references (one each in bold above) to get it to work for your case. See the first attached file.

If you have a cell which is changed by other cells (as you clarified in the discussion below), the If Intersect.. line can be changed to "see" only the cells used in the foirmula. For example, in the second attached file, there's a simple formula in cell B7:

=B9+B10*D10
and those cells are shaded green. I've modified the macro above so it does what it did (though the code is changed to a block If rather than a single If ..Then Exit...) and tests for changes in the cells used in the formula:
Private Sub Worksheet_Change(ByVal Target As Range)

' see if single cell was changed
If Intersect(Target, Range("B1")) Is Nothing Then
    ' if more than high cell, make high cell the new value
    If Range("B1").Value > Range("B3").Value Then Range("B3").Value = Range("B1").Value
End If

' see if cells used in formula cell have changed
If Intersect(Target, Range("B9,B10,D10")) Is Nothing Then Exit Sub
' if formula is more than formula high cell, make latter cell the new value
If Range("B7").Value > Range("B12").Value Then Range("B12").Value = Range("B7").Value

End Sub

Hope this helps.

Discuss

Discussion

Thanks John. Already reviewed Variatus answers - way too overkill for my question. I only have 2 specific cells in mind.
gest (rep: 2) Nov 12, '21 at 8:17 am
Please see my revised (if dashed) Answer / file.
John_Ru (rep: 6152) Nov 12, '21 at 9:00 am
Thanks John. I did that get to work in the case where B1 (your code example) is modified directly by me entering a different value. My problem is that B1 contains a formula and is only changed indirectly by values changing in other cells.
I am not a VBA person. Have spent today reading about worksheet events and came across the Worksheet_Calculate event. Seems like overkill since many cells are changed everytime I use the spreadsheet - it does seem to work however when I removed the Intersect line. Interested to learn if there was a better solution. Cheers
gest (rep: 2) Nov 13, '21 at 5:06 pm
Gest. I added the If Intersect... line since your question said a single cell changed (and that would capture manual changes to a given cell. Removing it means any change in the sheet would trigger the macro.

I'll revise my Answer to show and example.

The thing to remember next time is that if your question isn't accurate, you won't get a proper answer. It nearly always helps us if you upload a represntative Excel file, including your attempts (using the Add files... button in the question). 
John_Ru (rep: 6152) Nov 14, '21 at 3:10 am
Hi John. Thanks - and sorry for not being more precise in my question. I now have realized that a change that triggers the code - even an erroneous change - is persistent. Since I am making many changes I sometimes need to also make corrections. So I did more reading and found the BeforeClose event. This seems to fit my use case better. Thanks for your support. It has been 43 years since I last coded anything - I think I will explore VBA further - seems like a useful tool in excel. Cheers.
gest (rep: 2) Nov 14, '21 at 9:48 am
Glad that it helped (and that it gave you a peek at the power of VBA- with a bit of thought it can do wonderful things).

If the solution worked you should mark it as Selected according to the rules of the Forum (that guides other users and recognises my effort).

I also suggest that you look at the VBA section of the Tutorials part of this site- Don has put some super tutorials on there. 
John_Ru (rep: 6152) Nov 14, '21 at 11:14 am
Add to Discussion


Answer the Question

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