For Loop to find Maximum value in a column

iand5

New Member
Joined
Jul 26, 2017
Messages
36
How would you go about finding the maximum value in a column using a for loop?

I want to be able to store the maximum in a variable.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You could use the max worksheet function

Code:
Dim max As Integer


max = WorksheetFunction.max(Range("A2:A2000"))
 
Upvote 0
Perhaps a stupid question, but what if I want to select the range that containers the largest value. Is it possible to somehow select a dimension?
 
Upvote 0
Perhaps a stupid question, but what if I want to select the range that containers the largest value. Is it possible to somehow select a dimension?
Using mrhstn's code line from Messsage #2 as a base...

Columns("A").Find(WorksheetFunction.Max(Range("A2:A2000"))).Select
 
Upvote 0
If you want to select the first cell with the maximum value, maybe

Code:
Range("A1").Offset(WorksheetFunction.Match(WorksheetFunction.max(Range("A1:A2000")), Range("A1:A2000"), 0) - 1, 0).Select
 
Upvote 0
Why do you want to select it? It's not necessary to select ranges in order to work with them in VBA.
 
Upvote 0
How would you go about finding the maximum value in a column using a for loop?

I want to be able to store the maximum in a variable.
You ask specifically for a for loop ...here's one ...as you request, the maximum is stored in the variable maxval ...
Code:
Sub for_loop_for_max()

Dim a, i As Long, maxval, maxcell As Long
a = Range("A1:A2000").Value
maxval = a(2, 1)
For i = 2 To 2000
    If a(i, 1) > maxval Then
        maxval = a(i, 1)
        maxcell = i
    End If
Next i
Range("A" & maxcell).Select
MsgBox maxval & " is the maximum at row " & maxcell

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,836
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top