2017-06-23 39 views
0

我使用VBA來搜索一個電子表格字符串的外觀:找到只有在匹配的字符串是細胞的第一部分

Range("A:A").Find(What:="B ", After:=ActiveCell, LookIn:=xlValues, _ 
     LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _ 
     MatchCase:=True, SearchFormat:=False).Activate 

這工作得很好,但會拉起兩個「B XYZ 「和」xyzB abc「。我只想要第一個實例,在B前沒有任何東西。我知道如果我想找到第二個實例,我可以在我的What(例如「* B」)中使用通配符,是否存在「negative 「我可以用來表示」B「應該是細胞中第一個東西的性格?

謝謝!

回答

0

使用後通配符與xlWhole

Sub ngfdcvb() 
    Dim r As Range 
    Set r = Range("A:A").Find(What:="B*", After:=ActiveCell, LookIn:=xlValues, _ 
      LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _ 
      MatchCase:=True, SearchFormat:=False) 

    MsgBox r.Value 
End Sub 

enter image description here

+0

完美。不知道我是如何忽略這個簡單的解決方案!謝謝。 – Silver

相關問題