宏使用查找功能并返回单元格位置

时间:2014-03-31 09:47:00

标签: vba excel-vba excel

我试图通过两个输入(sheetname,word)实现一个函数并返回" word"的单元格位置。 in(row,column) 这是返回行值

的代码
    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

我没有设法找到" A"在代码中提到的F​​euil1中。每次执行代码时,我都会找到msg"找不到"。 - 我肯定有 -

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

问题是引号内的任何内容都将被视为字符串。

更改

rng.Find("word1"

rng.Find(word1

答案 1 :(得分:1)

你告诉它要查找“word1”(一个字母表示“word1”)而不是变量word1,删除引号。即 rng.Find(word1,lookat:= xlwhole)