在列Vba中查找范围内的最小值和最大值

时间:2018-09-05 19:12:17

标签: excel vba max min

我一直在尝试查找列的最小值和最大值,但似乎无法使我的代码正常工作。我已经尝试过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

1 个答案:

答案 0 :(得分:3)

尝试一下:

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