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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,181
Office Version
  1. 365
Platform
  1. Windows
If you want a formula, looking in column A: =ROW(INDEX(A2:A8,MATCH(MAX(A2:A8),A2:A8),1))
 
Upvote 0

Snakehips

Well-known Member
Joined
May 17, 2009
Messages
5,744
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,181
Office Version
  1. 365
Platform
  1. Windows
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,195,743
Messages
6,011,393
Members
441,610
Latest member
Shakeable_Drip

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
Top