Since you are looking up a certain pattern I thought this could be done through regular expressions since each match in the MatchCollection2
object will have a starting index including the length of the captured pattern. Let's imagine the following sample data:
Now we can apply the following code:
Sub Test()
Dim str As String: str = [A1]
Dim colMatch, objMatch
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "S+:(?=.*$)"
If .Test(str) = True Then
Set colMatch = .Execute(str)
For Each objMatch In colMatch
Range("A1").Characters(objMatch.FirstIndex, objMatch.Length).Font.Bold = True
Next
End If
End With
End Sub
The result:
About the regular expression's pattern:
S+:(?=.*$)
You can see an online demo here and a small breakdown below:
S+:
- 1+ Non-whitespace character up to and including a colon.
(?=
- A positive lookahead:
.*$
- 0+ characters other than newline up to the end string anchor.
)
- Close positive lookahead.
Note: We need to either forget about the "Newline" property of the regex object or set it's value to FALSE
. In the example I gave I simply didn't include it because it will then default to FALSE
. If this was set to true the end string anchor won't simply match the end of the whole string but the end of each line (which is what we want to avoid if we don't want to match "Server:").
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…