Regex 在Excel中为单元格内的部分文本着色

Regex 在Excel中为单元格内的部分文本着色,regex,excel,vba,batch-processing,Regex,Excel,Vba,Batch Processing,我需要将[方括号]中的所有内容和这些内容中的所有HTML/XML标记在选中的表格中的所有单元格中以通用红色显示。单元格中的其余文本需要保持黑色 我试图修改附加的代码,但只能使括号变为红色,而文本的其余部分保留为黑色。我想我需要添加正则表达式范围\[.*?\]和\但不确定如何添加。请帮忙 Sub Format_Characters_In_Found_Cell() Dim Found As Range, x As String, FoundFirst As Range x = "[" y = "]

我需要将[方括号]中的所有内容和这些内容中的所有HTML/XML标记在选中的表格中的所有单元格中以通用红色显示。单元格中的其余文本需要保持黑色

我试图修改附加的代码,但只能使括号变为红色,而文本的其余部分保留为黑色。我想我需要添加正则表达式范围\[.*?\]和\但不确定如何添加。请帮忙

Sub Format_Characters_In_Found_Cell()
Dim Found As Range, x As String, FoundFirst As Range

x = "["
y = "]"

On Error Resume Next
Set Found = Cells.Find(what:=x, LookIn:=xlValues, LookAt:=xlPart)
If Not Found Is Nothing Then
    Set FoundFirst = Found
    Do
      'Format "x"
        With Found.Characters(Start:=InStr(Found.Text, x), Length:=Len(y))
            .Font.ColorIndex = 3
            .Font.Bold = False
        End With
        Set Found = Cells.FindNext(Found)
    Loop Until FoundFirst.Address = Found.Address
Else
    MsgBox x & " could not be found.", , " "
End If 
End Sub
Leny当y包含单个字符时,将始终返回值1

正确的长度是字符串中x和y之间的字符数,因此需要使用以下内容:

With Found.Characters(Start:=InStr(Found.Text, x), _
                      Length:=Instr(Found.Text, y) - Instr(Found.Text, x) + 1)
或者,如果不想给括号本身上色,可以在起始位置加1,从长度中减去2,从而得出:

With Found.Characters(Start:=InStr(Found.Text, x) + 1, _
                      Length:=Instr(Found.Text, y) - Instr(Found.Text, x) - 1)
为了兼顾[…]和我的偏好,我会修改子例程,以允许将正在搜索的括号类型作为参数传递,然后调用子例程两次

Sub Test
    Format_Characters_In_Found_Cell "[", "]"
    Format_Characters_In_Found_Cell "<", ">"
End Sub

Sub Format_Characters_In_Found_Cell(x As String, y As String)
Dim Found As Range, FoundFirst As Range

On Error Resume Next
Set Found = Cells.Find(what:=x, LookIn:=xlValues, LookAt:=xlPart)
If Not Found Is Nothing Then
    Set FoundFirst = Found
    Do
      'Format "x"
        With Found.Characters(Start:=InStr(Found.Text, x), _
                              Length:=Instr(Found.Text, y) - Instr(Found.Text, x) + 1)
            .Font.ColorIndex = 3
            .Font.Bold = False
        End With
        Set Found = Cells.FindNext(Found)
    Loop Until FoundFirst.Address = Found.Address
Else
    MsgBox x & " could not be found.", , " "
End If 
End Sub

Leny可能应该是InstrFound.Txt,y-InstrFound.Txt,x+1,或者,如果您不希望[and]本身被涂成红色,请使用Start:=InstrFound.Text,x+1,Length:=InstrFound.Txt,y-InstrFound.Txt,x-1@YowE3K如果我将设置为:With Found.CharactersStart:=InStrFound.Text,x,Length:=InStrFound.Txt,则它根本不会以这种方式工作,y-InStrFound.Txt,按照您的说明x+1。有什么想法吗?在这一行:With Found.CharactersStart:=InStrFound.Text,x,Length:=Leny长度:=值为1。你需要它是变红的字符数之间的距离。是的-我有个主意-让我参加打字课。Txt应该是文本。在创建答案之前,我是在测试时发现它的。这很有魅力,谢谢!是否可以将HTML/XML标记和变量{}也包括在x=[| |}字符串?不知道如何将它们添加到此处以避免创建多个模块…@Ilia-您上一次的评论被打断,因为我假设使用@Ilia-的正则表达式可以让您一次处理所有括号类型,但是很难一次找到它们。您可能必须处理所有单元格,而不仅仅是那些在Find locates,我认为多次使用Find会比使用Find更有效。同意。在您的解决方案中添加了4个sumbodules,以解释所有括号变化和变量表达式。谢谢!@ilia-是因为处理不正确,还是因为所有情况下都会发生多次错误单元格中括号的大小写,例如gdashgd[hadkjdh]adakhsdlk[asdkajh]asgdjahg。当前代码将仅突出显示找到的第一个括号。
Sub Format_Characters_In_Found_Cell(x As String, y As String)
Dim Found As Range, FoundFirst As Range
Dim posStart As Long
Dim posEnd As Long

On Error Resume Next
Set Found = Cells.Find(what:=x, LookIn:=xlValues, LookAt:=xlPart)
If Not Found Is Nothing Then
    Set FoundFirst = Found
    Do
      'Format "x"
        posStart = InStr(Found.Text, x)
        Do While posStart > 0
            posEnd = InStr(posStart + 1, Found.Text, y)
            If posEnd = 0 Then
                Exit Do ' no matching end bracket
            End If
            With Found.Characters(Start:=posStart, Length:=posEnd - posStart + 1)
                .Font.ColorIndex = 3
                .Font.Bold = False
            End With
            posStart = InStr(posEnd + 1, Found.Text, x)
        Loop
        Set Found = Cells.FindNext(Found)
    Loop Until FoundFirst.Address = Found.Address
Else
    MsgBox x & " could not be found.", , " "
End If
End Sub
Sub Format_Characters_In_Found_Cell()
Dim Found As Range, x As String, FoundFirst As Range

x = "["
y = "]"

On Error Resume Next
Set Found = Cells.Find(what:=x, LookIn:=xlValues, LookAt:=xlPart)
If Not Found Is Nothing Then
    Set FoundFirst = Found
    Do
      'Format "x"
        l = InStr(Found.Text, y) - InStr(Found.Text, x) + 1
        With Found.Characters(Start:=InStr(Found.Text, x), Length:=l)
            .Font.ColorIndex = 3
            .Font.Bold = False
        End With
        Set Found = Cells.FindNext(Found)
    Loop Until FoundFirst.Address = Found.Address
Else
    MsgBox x & " could not be found.", , " "
End If
End Sub