vba 使用 Find 函数并返回单元格位置的宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22758439/
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
Macro using Find function and returning cell position
提问by ThinkMAL
I'm trying to realize a function with two inputs (sheetname, word) and returning the cell location of "word" in (row , column) Here is the code for returning the row value
我正在尝试实现一个具有两个输入(工作表名称,单词)并返回“单词”在(行,列)中的单元格位置的函数这是返回行值的代码
Function RowFinder(sheet1 As String, word1 As String) As Integer
Dim rng As Range
Dim rngFound As Range
' I know that the word is situated btw A and C
Set rng = Sheets(sheet1).Range("A:C")
Set rngFound = rng.Find("word1", LookAt:=xlWhole, SearchOrder:=xlByRows)
If rngFound Is Nothing Then
MsgBox "not found"
Else:
MsgBox "found"
RowFinder = rngFound.row
End If
End Function
Private Sub CommandButton1_Click()
Call RowFinder("Feuil1", "A")
End Sub
I didn't manage to find "A" in Feuil1 as mentioned in the code. each time I execute the code, I have the msg "not found". -I'm sure of having it -
如代码中所述,我没有设法在 Feuil1 中找到“A”。每次我执行代码时,我都会收到“未找到”的味精。- 我肯定会拥有它 -
Thanks for help!
感谢帮助!
回答by Siddharth Rout
The problem is anything within quotes will be considered as a string.
问题是引号内的任何内容都将被视为字符串。
Change
改变
rng.Find("word1"
to
到
rng.Find(word1
回答by Phil D
You're telling it to look for "word1" (a string that says "word1") not the variable word1, remove the quotation marks. i.e. rng.Find(word1, lookat:=xlwhole)
您告诉它查找“word1”(表示“word1”的字符串)而不是变量 word1,删除引号。即 rng.Find(word1, lookat:=xlwhole)