返回绝对参考号码

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

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

我曾尝试使用匹配,但我得到错误2042年。

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

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

请注意,我正在寻找时间戳,因此CLNG包装rng不是一个选项。

尝试这个:

 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 

您需要确保您使用正确的引用。

例如,如果代码运行在Sheet1和数据在Sheet2 ,则可能会出现错误。 以此为例,当代码和数据位于不同的WorkSheets时,我得到了adrsvariables的Error 2042

另外,永远记得Option Explicit这将迫使你去改变你的variables。

尝试以下操作:

 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 

或者只是这个?

 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