在vba中找到2个字符和格式之间的任何内容

时间:2016-09-27 12:25:22

标签: excel vba format find

我想在2个字符之间找到一个文本,并且只能找到文本格式 例如: 单元格a1 =你好!这是考验!森泰斯 我想只改变感叹号之间的部分。

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
`

1 个答案:

答案 0 :(得分:1)

没有Regex,在字符上使用简单的循环;选择要处理和运行的单元格:

{{1}}

在:

enter image description here

之后:

enter image description here

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