vba 在 Excel 中获取给定列号和单元格数据的行号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39520111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Row Number in Excel for given Column Number and Cell Data
提问by newuser
If I know the cell data and the column number where i expect the data to be in. Please let me know how do i retrieve the row number for that cell. Thank you.
如果我知道单元格数据和我希望数据所在的列号。请告诉我如何检索该单元格的行号。谢谢你。
回答by
Use an Excel Application object's use of a MATCH function.
使用Excel Application 对象对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
回答by Gary's Student
Here is one way for column B:
这是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
EDIT#1:
编辑#1:
This version uses parameters for the value to find, and the column to 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