2012-02-15 91 views
0

我有兩個問題。這需要一些方向。通配符匹配框

1)如何在VBA中使用LIKE運算符時使用Matchcase。下面的代碼只匹配單詞的確切形式。我想我將不得不使用.MatchWildcards = False,但不知道如何/在哪裏使用它。 例如:搜索'Texas',但這不考慮'texas'。

2)我只需要找到'德克薩斯'這個詞,而不是'TexasRangers'。有沒有辦法做到這一點,我該如何修改我的代碼。

Sub Example() 
    Dim wsh As Worksheet, i As Long, lngEndRowInv As Long 
    Set wsh = ActiveSheet 
    i = 2 
    Lastr= wsh.Range("A" & wsh.Rows.Count).End(xlUp).Row 
    While i <= Lastr 
     If (Cells(i, "F") Like "*New Orleans*") And (Cells(i, "D") Like "*Belfast*") Then 
      Cells(i, "C").Value = "Deleted" 
      Cells(i, "C").Font.Color = vbRed 
     ElseIf Not ((Cells(i, "A") Like "*Texas*") Or (Cells(i, "A") Like "*NY*")) Then 
      Cells(i, "A").Value = "Not Deleted" 
     End If 
     i = i + 1 
     Wend 
End Sub 

回答

2

MatchWildcards是查找的一部分和/替換對象模型,因此它不適用於VBA的LIKE

對於情況靈敏度,力的特定情形;

... ucase$((Cells(i, "A")) Like "*TEXAS*" 

如果你想看看是否單元格中包含一個整詞,而佔周邊空白(東西「LIKE」真的不能做)即可;

dim re: set re = CreateObject("VBScript.RegExp") 
re.pattern="\bTEXAS\b" 
re.ignorecase=true 

if re.test(Cells(i, "A")) then 
    //cell contains "TEXAS" in any case surrounded by a word boundary (white space, start/end of line). 
+0

感謝@alex K.你能告訴我如何在這段代碼中實現第二點。我有四個搜索執行,你可以在我的上面的代碼中看到。 P.S我仍然在學習這些新概念。謝謝你的時間。 – User124726 2012-02-15 15:45:55

+0

使用第二部分的正則表達式有什麼問題? – 2012-02-15 16:03:29