find2个字符之间的任何内容,并在vba格式

我想find一个2个字符之间的文本,并格式化文本只是例子:cell a1 = hello! 这是testing! 我只想改变感叹号之间的部分。

Sub Macro2() Dim varFound As Variant, varSearch As Variant Dim strAddress As String, intPos As Integer varSearch = "!*!" Set varFound = Cells.Find(varSearch, LookIn:=xlValues, LookAt:=xlPart) If Not varFound Is Nothing Then strAddress = varFound.Address Do With varFound Do intPos = InStr(intPos + 1, .Value, varSearch, vbTextCompare) If intPos Then .Characters(Start:=intPos, Length:=Len(varSearch)).Font.FontStyle = "Bold" .Characters(Start:=intPos, Length:=Len(varSearch)).Font.ColorIndex = 3 End If Loop Until intPos = 0 End With Set varFound = Cells.FindNext(varFound) Loop While Not varFound Is Nothing And _ varFound.Address <> strAddress End If End Sub ` 

没有正则expression式,在字符上使用一个简单的循环; select你想要处理和运行的单元格:

 Sub poiuyt() Dim r As Range, st As String, boo As Boolean Dim L As Long, i As Long For Each r In Selection st = r.Text boo = False L = Len(st) For i = 1 To L If Mid(st, i, 1) = "!" Then boo = Not boo Else If boo Then r.Characters(i, 1).Font.Bold = True End If Next i Next r End Sub 

之前:

在这里输入图像说明

之后:

在这里输入图像说明

更改代码以反映您需要的任何字符格式。