我正在尝试编写一个名为stringExists的布尔函数。该函数有两个参数:一个名为rng的Range和一个名为srch的String。如果在范围rng中找到字符串srch,则该函数返回True。
这是我到目前为止的代码:
Function stringExists(rng As Range, srch As String) As Boolean
if srch isFound in rng then
stringExists = True
else
stringExists = False
End Function
然而,如果srch isFound在我的vba编辑器中突出显示为红色。我很确定我已经掌握了这段代码的基本知识,但我很难知道我的错误是什么。希望得到关于此事的一些指导。谢谢!
此处编辑是我目前所拥有的:
Sub test()
Dim rng As Range
Dim srch As String
srch = InputBox("Enter the string you want to search for: ")
Set rng = Range("A1:A100")
MsgBox "The phrase is" & _
stringExists(rng, srch)
End Sub
Function stringExists(rng As Range, srch As String) As Boolean
If InStr(rng, srch) Then
stringExists = True
Else
stringExists = False
End If
End Function
答案 0 :(得分:0)
你可以这样试试......
Function stringExists(rng As Range, srch As String) As Boolean
If InStr(rng, srch) Then
stringExists = True
Else
stringExists = False
End If
End Functio
如果您将单个单元格作为第一个参数传递,则上述函数将起作用。 如果你的范围包含多个单元格,请尝试这样的函数......
Function stringExists(rng As Range, srch As String) As Boolean
Dim cell As Range
For Each cell In rng
If InStr(cell, srch) Then
stringExists = True
Exit For
Else
stringExists = False
End If
Next cell
End Function