2017-06-06 33 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邊緣的情況下可能是防彈檢查的最佳步驟。

相關問題