简体   繁体   中英

Max Min of dynamic cell not updating when worksheet/workbook in background

How to find max and min of a dyanamic cell in Excel Hi all, with regard to the above vba code, I tried the code but the max/min cell only updates if I click on the dynamic cell and then click away. If the workbook is not active and is in the background while I work on other programs,the min/max cells wouldn't update. How do I fix this? Thank you in advance.

This can be achieved with a regular formula, but requires Iterative Calculation to be enabled.

This is set under Options/Formulas

在此处输入图像描述

With this set, The Max formula should reference the cell(s) you want to monitor, and itself.

Eg if your Max is in D3 in Sheet1 and is monitoring B3 on Sheet2 then use

=MAX(Sheet2!B3,D3)

Note: stored Max value will survive the workbook being closed and reopened.

Thic can be achieved with a volitile UDF , using a Static variable to remember the last Max value

Function MyMax(r As Range) As Variant
    Application.Volatile True
    Static LastMax As Variant
    Dim NewMax As Variant
    NewMax = Application.WorksheetFunction.Max(r)
    If NewMax > LastMax Then
        LastMax = NewMax
    End If
    MyMax = LastMax
End Function

Note:

  1. stored Max value won't survive the workbook being closed and reopened. To achieve that you'd need to store the value somewhere else, eg in an out of the way cell
  2. This formula only stores one Max value. If you want to use the formula >1 times in your workbook, each with its own Max, you'll need to store each Max seperately, perhaps in a Collection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM