Excel 2007 VBA获取活动单元格相对于其在某个范围内的位置的行号

时间:2013-09-08 14:16:27

标签: excel vba excel-2007

我一直在努力解决这个问题,我的谷歌搜索页面充满了访问过的链接,因为我尝试用一​​百种不同的方式搜索我的搜索,但没有成功!

我有一个范围,比如说A8:A17,活动单元格是A10。如何获得A10相对于范围中第一个单元格的行号A8(我希望结果为3)?我想使用此值来使用Range(exRange).Cells(exRow, 1)引用另一个单元格。我能想到的唯一方法是循环遍历范围,直到循环数等于活动单元格的行,但必须有一个更简洁的方法!

2 个答案:

答案 0 :(得分:3)

使用属性:

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

答案 1 :(得分:-1)

怎么样:

=MATCH(A10, $A$:$A18$, 0) //returns 3
相关问题