簡體   English   中英

Excel 2007 VBA獲取相對於活動單元格在區域中的位置的行號

[英]Excel 2007 VBA Get the row number of an active cell relative to its position in a range

我一直在想辦法解決這一問題,甚至擊敗了自己,我的google搜索頁面上充滿了訪問過的鏈接,因為我嘗試用一​​百種不同的方式寫我的搜索,但沒有成功!

我有一個范圍,例如A8:A17 ,活動單元格是A10 如何獲得相對於范圍A8第一個單元格的A10行號(我希望結果是3 )? 我想使用此值使用Range(exRange).Cells(exRow, 1)引用另一個單元格。 我能想到的唯一方法是在范圍內循環,直到循環號等於活動單元格的行,但是必須有一種更干凈的方法!

使用屬性:

Sub bafedm()
    Dim Tabl As Range, r As Range
    Set Tabl = Range("A8:A17")
    Set r = Range("A10")
    MsgBox r.Row - Tabl.Row + 1
End Sub

作為參考,下面是一些代碼,用於說明典型矩形范圍的其他屬性和尺寸:

Sub range_reporter()
    Dim r As Range
    Dim nLastRow As Long, nLastColumn As Long
    Dim FirstRow As Long, nFirstColumn As Long

    ActiveSheet.UsedRange
    Set r = ActiveSheet.UsedRange

    nLastRow = r.Rows.Count + r.Row - 1
    MsgBox ("last row " & nLastRow)

    nLastColumn = r.Columns.Count + r.Column - 1
    MsgBox ("last column " & nLastColumn)

    nFirstRow = r.Row
    MsgBox ("first row " & nFirstRow)

    nFirstColumn = r.Column
    MsgBox ("first column " & nFirstColumn)

    numrow = r.Rows.Count
    MsgBox ("number of rows " & numrow)

    numcol = r.Columns.Count
    MsgBox ("number of columns " & numcol)
    End Sub

怎么樣:

=MATCH(A10, $A$:$A18$, 0) //returns 3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM