首页 > 解决方案 > 从动态单元格中获取最高和最低值

问题描述

我一直试图从单元格 AO1 到 AP2 的单元格 R1 和 R2 中获取最高和最低值。我能够在 AO1 和 AO2 中获得最高值,但它没有在 AP1 和 AP2 中记录最低值。一旦在 AP1 和 AP2 中输入等效数字(R1 和 R2 的值),最低值开始记录,当尝试调试时,R1 和 R2 中的值显示,但 AP1 和 AP2 为“空”。

有人可以帮忙吗。这就是我到目前为止所得到的。

 Private Sub Worksheet_Calculate()
'Highest Value

 Dim r1 As Range: Set r1 = Range("R1")
 Dim r2 As Range: Set r2 = Range("R2")

'Lowest Value

Dim r1a As Range: Set r1a = Range("R1")
Dim r2a As Range: Set r2a = Range("R2")

Application.EnableEvents = False

'This registers the highest value

    If r1.Value > ao1.Value Then
       ao1.Value = r1.Value
    End If
        
    If r2.Value > ao2.Value Then
       ao2.Value = r2.Value
    End If

   'This registers the lowest value

    If r1a.Value < ap1.Value Then
    ap1.Value = r1a.Value
    End If
    
    If r2a.Value < ap2.Value Then
    ap2.Value = r2a.Value
    End If
    
    Application.EnableEvents = True
 End Sub

标签: excelvba

解决方案


推荐阅读