首页 > 解决方案 > 在 Vba 列的范围内查找最小值和最大值

问题描述

我一直在尝试查找列的最小值和最大值,但似乎无法让我的代码正常工作。我已经尝试过 if 语句,for 循环,但似乎无法使其工作。我也一直在使用 application.worksheetfunction.min/max 并且无法正常工作。

Sub MinMax()
    Dim xmax As Double
    Dim xmin As Double
    Dim TableRow As Integer

    For i = 2 To lastrow   
        If cells("i,11").Value < cells(i + 1, 11).Value Then
            xmin = cells(i, 11).Value
            cells(3, 16).Value = xmin
        End If

        If cells(i, 11).Value > cells(i + 1, 11).Value Then
            cells(2, 16).Value = xmax
        End If
    Next i
End Sub

标签: excelvbamaxmin

解决方案


试试这个:

Sub MinMax()

    Dim xmax As Double
    Dim xmin As Double

    Dim r As Range

    Set r = Range("K2:K" & Rows.Count)
    xmin = Application.WorksheetFunction.Min(r)
    xmax = Application.WorksheetFunction.Max(r)
End Sub

推荐阅读