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

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

这是列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:

此版本使用要查找的值的参数以及要search的列:

 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 

使用Excel应用程序对象使用MATCH函数 。

 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