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

Get Row Number in Excel for given Column Number and Cell Data

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

使用 Excel Application object's use of a MATCH function.

dim rw as variant
with worksheets("Sheet1")
    rw = application.match(<value_to_find>, .columns(1), 0)  'column A
    if iserror(rw) then
        'not found - rw is a worksheet error code
    else
        'found - rw is a long integer representing the row number
    end if
end with

这是第 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

编辑#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