在Excel中获取给定列号和单元格数据的行号

时间:2016-09-15 20:47:49

标签: excel vba excel-vba vbscript hp-uft

如果我知道单元数据和我期望数据所在的列号。请让我知道如何检索该单元格的行号。 谢谢。

2 个答案:

答案 0 :(得分:4)

答案 1 :(得分:4)

以下是 B 列的一种方法:

Sub HappinessRow()

Dim r As Range

Set r = Range("B:B").Find(what:="happiness", after:=Range("B1"))
If r Is Nothing Then
    MsgBox "could not find happiness"
    Exit Sub
End If
MsgBox "happiness found in row " & r.Row
End Sub

enter image description here

修改#1:

此版本使用要查找的值的参数和要搜索的列:

Sub HappinessRow2()

    Dim r As Range, s As String, kolumn As Long

    s = "happiness"
    kolumn = 2

    Set r = Cells(1, kolumn).EntireColumn.Find(what:="happiness", after:=Cells(1, kolumn))
    If r Is Nothing Then
        MsgBox "could not find happiness"
        Exit Sub
    End If

    MsgBox "happiness found in row " & r.Row
End Sub