简体   繁体   English

是否有一个 excel/VBA 公式来查找长度不同的一系列单元格的最小/最大/平均值等?

[英]Is there an excel/VBA formula to find the min/max/average etc of a range of cells that varies in length?

I would like to know if there is a way to find something like the min/max/average etc of a range of cells within a spreadsheet, when I don't know where the end of the range is (I already know one way but wondering if there is another)?我想知道是否有一种方法可以找到电子表格中一系列单元格的最小/最大/平均值等,当我不知道范围的结尾在哪里时(我已经知道一种方法但是想知道是否还有另一个)?

Within my macro I currently use the following:在我的宏中,我目前使用以下内容:

Raw1.Range("H4").Value = Application.WorksheetFunction.Max(Range(Sheets("Raw").Range("A3"), Sheets("Raw").Range("A3").End(xlDown)))

I would like to use different then this, because this way gives "one of value" each time the code is run...I want the value of H4 to change each time the values of A3 changes without having to run the macro...So more like a formula我想使用不同的,因为这种方式每次运行代码时都会给出“一个值”......我希望每次 A3 的值发生变化时 H4 的值都会改变,而不必运行宏。 .所以更像是一个公式

I was thinking something like below:我在想像下面这样的事情:

Raw1.Range("H3").Value = "= Min((A3:A & Range.End(xlDown).Row))"

I have tried various things but can not get it to work?我尝试了各种方法但无法使其正常工作?

Does anyone know if this is possible?有谁知道这是否可能?

Since you don't want actively to have to run a macro, the suggestion below is coded in the Worksheet_Change() event procedure :由于您不想主动运行宏,因此以下建议在Worksheet_Change()事件过程中编码:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Range("A:A"), Target) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        Dim lastCell As Long, pos As Long, formulaCell As Range
        Set formulaCell = Range("H4")
        lastCell = Cells(Rows.Count, 1).End(xlUp).Row
        pos = InStr(1, formulaCell.Formula, "(") - 1
        formulaCell.Formula = Left(formulaCell.Formula, pos) & "(A2:A" & lastCell & ")"
    End If
End Sub

it is premised on the examples in your OP, in that the cell in question contains a simple formula (ie the address of the referenced cells comes after the 1st ( of the existing formula, and there is nothing, other than the ) after this reference), the range reference for which the event procedure will update whenever a change in column A is detected.它以您的 OP 中的示例为前提,因为有问题的单元格包含一个简单的公式(即)引用的单元格的地址位于现有公式的第一个( ,除了),每当检测到 A 列发生变化时,事件过程将更新的范围参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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