How can I return the row number of a maximum value in a range in VBA?

dan7055

Active Member
Joined
Jul 9, 2015
Messages
312
I can use the worksheetfunction.max() to get the maximum value in a range, but how can I instead return the row number of said maximum value?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
I can use the worksheetfunction.max() to get the maximum value in a range, but how can I instead return the row number of said maximum value?

Code:
myval = Application.Max(Range("A1:A10")
Range("A1:A10").Find(myVal, , xlValues).Row
 
Upvote 0
If you want a formula, looking in column A: =ROW(INDEX(A2:A8,MATCH(MAX(A2:A8),A2:A8),1))
 
Upvote 0
Maybe if range is dynamic....

Code:
Set Rng = Range("D3:D15")   'or whatever
Mx = WorksheetFunction.Max(Rng)
Rw = WorksheetFunction.Match(Mx, Rng, 0) + Rng.Row - 1
 
Upvote 0
As with any method, error catching should be done. For simplicity, it was not done in our solutions.

While one can do a formula and Evaluate it in VBA, I would just go with the Find method as #2 and #4 posts show.

Code:
MsgBox Range("A1:A10").Find(WorksheetFunction.Max(Range("A1:A10"))).Row
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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