2017-06-06 35 views
0

我试图用一些VBA代码找到,如果说英语6个月在一个小区中,例如检查字符串多个单词(以任意顺序)

“英语测试6个月”
6个月英语

但不

6个月工程lishman

我只能找到一个词,发现在细胞

  • 两个单独字符串以任意顺序
  • 全字

是混淆了我。

+2

向我们展示您尝试过的以及遇到的问题。这不是一个免费的代码写入服务,但我们可以帮助您使用复杂的公式或代码来尝试开发。请阅读[我如何提出一个好问题](http://stackoverflow.com/help/how-to-ask)以及[如何创建最小,完整和可验证示例]的帮助主题(http ://stackoverflow.com/help/mcve) –

回答

4

更新 - Bulletproofed

Function StrCheck2(rng1 As Range, str1 As String, str2 As String) As Boolean 
Dim objRegex As Object 
Set objRegex = CreateObject("vbscript.regexp") 
With objRegex 
    .Pattern = "^(?=.*\b" & str1 & "\b)(?=.*\b" & str2 & "\b).*$" 
    .ignorecase = True 
    StrCheck2 = .test(rng1.Value2) 
End With 
End Function 

你可以做这样的事情

Function StrCheck(rng1 As Range, str1 As String, str2 As String) As Boolean 
If InStr(rng1.Value2, str1) > 0 Then 
    If InStr(rng1.Value, str2) > 0 Then StrCheck = True 
End If 
End Function 

分隔两个IFS允许提前退出,如果没有找到的第一个字符串。

你会这样称呼它 =StrCheck(A1,"English","6 months")

或避免部分匹配单词 = StrCheck(A1," English "," 6 months ") 这仍然需要修补的,其中英语可能是第一个字

一个Regexp边缘的情况下可能是防弹检查的最佳步骤。

相关问题