2012-02-15 122 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