返回绝对参考号

时间:2017-07-20 10:34:14

标签: excel vba excel-vba

我正在使用关注代码来查找列中的最大值。我需要知道找到该值的绝对引用号。我希望在FOR循环中使用该绝对引用号,并检查相邻单元格中找到该值的位置。

rng = Application.WorksheetFunction.Max(Columns("H")) 

我尝试过使用匹配但是我收到错误2042.

adrs = Application.Match(rng, Range("H:H"), 0)

有没有办法找出最大值所在的位置?

请注意我正在搜索时间戳,因此无法在CLNG中包装rng。

3 个答案:

答案 0 :(得分:0)

试试这个:

Dim rng As Range
Dim maxValue As Integer
'you set your area that you get mxaimum value from
Set rng = Range("H:H")
'determine the maximum value
maxValue = Application.WorksheetFunction.Max(rng)
'select cell which contains found value
'(Find returns Range objects, so you can use it as you like)
rang.Find(maxValue).Select

答案 1 :(得分:0)

您需要确保使用正确的参考资料。

例如,如果代码运行Sheet1并且数据位于Sheet2,那么您可能会收到错误。以此为例,当代码和数据位于不同的Error 2042时,adrs变量获得WorkSheets

另外,请记住Option Explicit这会迫使你去处理变量。

尝试以下方法:

Option Explicit

Sub FindMaxValueAndReturnRowNum()

    Dim SomeWorkSheet As Worksheet
    'Here you can change the Sheet1 to that of you data sheet name
    Set SomeWorkSheet = ThisWorkbook.Sheets("Sheet1")

    Dim MaxNum As Long
    Dim adrs As Long

    'Notice the reference to where the code needs to evaluate
    'if there is no reference to a specific sheet then it will use the active sheet.
    MaxNum = Application.WorksheetFunction.Max(SomeWorkSheet.Columns("H"))
    'adrs will return the row number
    adrs = Application.Match(MaxNum , SomeWorkSheet.Range("H:H"), 0)

End Sub

答案 2 :(得分:0)

或者只是这个?

Dim rng As Range
Set rng = Columns("H").Find(Application.Max(Columns("H")))
If Not rng Is Nothing Then
    MsgBox rng.Address(0, 0)
End If