在VBA中使用InStr进行多字符串搜索 [英] Multiple string search with InStr in VBA
本文介绍了在VBA中使用InStr进行多字符串搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在检查姓名"文本框是否以太太夫人"等开头.
I am checking whether a Name textbox starts with Mr. Mrs. Ms. etc.
我创建了一个函数,但不能比较多个字符串.
I created a function but I am not able to compare more than one string.
这是我的代码.
'Checking whether name is starts with Mr./Mrs./Ms./Dr. or not
If Not FindString(LCase(Me.gname.Value), LCase("Mr")) Then
MsgBox "Consumer Name Starts with Mr./Mrs./Ms./Dr. Check Consumer Name"
Cancel = True
Exit Sub
End If
'Here is the Find String function i created
Function FindString(strCheck As String, strFind As String) As Boolean
Dim intPos As Integer
intPos = 0
intPos = InStr(strCheck, strFind)
FindString = intPos > 0
End Function
推荐答案
将strFind作为由定界符分隔的字符串组传递,例如:-
Pass strFind as group of strings seperated by a delimiter ex:-
FindString(LCase(Me.gname.Value), LCase("Mr;Mrs;Ms;Dr"))
现在将它们拆分并使用循环进行比较.
Now split them and compare using a loop.
Arr = Split(strFind,";")
Flag = 0
For Each str in Arr
If InStr(strCheck, str) > 0 Then
Flag = 1
End If
Next
If Flag = 1 Then
FindString = True
Else
FindString = False
End If
这篇关于在VBA中使用InStr进行多字符串搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文