VBA单元格地址Max()

时间:2011-03-07 03:44:50

标签: excel vba excel-vba

我有类似

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

找到列D的最大数量

如何找到此最大数字的位置?

1 个答案:

答案 0 :(得分:12)

在vba中定义为用户定义的函数,将地址作为字符串

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

End Function

或返回范围参考

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

End Function

这些函数假设rng是一列宽

这些功能可以在纸张中使用 例如

=AddressOfMax(C:C)

或在vba中 例如

Dim r As Range
Set r = AddressOfMax(Range("D2", Cells(emptyRow, 4)))