S. Caruso
S. Caruso

Reputation: 39

Finding Min and max in a range in a column Vba

I have been trying to find the min and max of a column and Cannot seem to get my code to work correctly. I have tried if statements, for loops and cannot seem to make it work. I have also been using application.worksheetfunction.min/max and have not been able to get anything to work.

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

Upvotes: 0

Views: 5603

Answers (1)

Pm Duda
Pm Duda

Reputation: 740

Try this:

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

Upvotes: 3

Related Questions