简体   繁体   中英

How to find max and min of a dyanamic cell in Excel

I wish to have 3 cells in excel.

One is dynamic and max and min cell.

So whenever the dynamic value goes to max, the max cell should be updated, else the max value should hold the previous highest value.

Similarly for min as well.

VBA = Visual Basic for Applications. To access it, open Excel and press Alt + F11 . In the Visual Basic Editor (VBE), double click on the sheet name that will have the dynamic cell. In this example, it is just Sheet1. After double-clicking on that sheet name, a code window will open on the right hand side. At the top of the code window, use the two drop downs select Worksheet (in order to tell it you want a worksheet event) and then Change (to tell it you want the Worksheet_Change event. It will auto create a line with Private Sub Worksheet_Change(ByVal Target as Range) and will look something like this:

在此处输入图像描述

Then just copy this code and paste it in between the Private Sub... and End Sub lines so that it matches the picture:

Dim rDynamic As Range
Dim rMax As Range
Dim rMin As Range

Set rDynamic = Me.Range("A1")
Set rMax = Me.Range("B1")
Set rMin = Me.Range("C1")

If Not Intersect(Target, rDynamic) Is Nothing Then
    If rDynamic.Value < rMin.Value Then rMin.Value = rDynamic.Value
    If rDynamic.Value > rMax.Value Then rMax.Value = rDynamic.Value
End If

And test it out. To save the workbook, you'll need to use the .xlsm file format, and you will also need to enable macros (Excel should prompt you to enable or disable macros after saving, closing, and reopening the workbook).

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