Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Cell address of Max()

Tags:

excel

vba

I have something like

sdMax = WorksheetFunction.Max(Range("D2", Cells(emptyRow, 4)))

to find the maximum number of column D

How do I find the location of this maximum number?

like image 643
lamba Avatar asked Mar 07 '11 03:03

lamba


People also ask

How do I find the cell address in Excel VBA?

For example: =ADDRESS(1,1) - returns the address of the first cell (i.e. the cell at the intersection of the first row and first column) as an absolute cell reference $A$1. =ADDRESS(1,1,4) - returns the address of the first cell as a relative cell reference A1.

How do you find the address of a cell with maximum or minimum in Excel?

1. To get the address of the cell with smallest value of this column, please apply this formula: =CELL("address",INDEX(A2:A15,MATCH(MIN(A2:A15),A2:A15,0))). 2. In above formula A2:A15 is the column cells that you want to use, you can change it to your need.

Is there a max function in VBA?

Max function is used to find the maximum value out of a range of values. It is an inbuilt function in Excel and categorized as the Max function. However, in VBA, there is no inbuilt function as Max to get the maximum value. Max function can be used in VBA Excel also.


1 Answers

Defined as a user defined function in vba, returning the address as a string

Function AddressOfMax(rng As Range) As String
    AddressOfMax = WorksheetFunction.Index(rng, WorksheetFunction.Match(WorksheetFunction.Max(rng), rng, 0)).Address

End Function

Or returning a range reference

Function AddressOfMax(rng As Range) As Range
    Set AddressOfMax = rng.Cells(WorksheetFunction.Match(WorksheetFunction.Max(rng), rng, 0))

End Function

these functions assume rng is one column wide

These functions can be used in the sheet
eg

=AddressOfMax(C:C)

or in vba
eg

Dim r As Range
Set r = AddressOfMax(Range("D2", Cells(emptyRow, 4)))
like image 141
chris neilsen Avatar answered Oct 09 '22 18:10

chris neilsen