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

我需要在[方括号]中的所有内容以及所有单元格中选定工作表上的所有通用红色<brackets> HTML / XML标记。 单元格中的其他文本需要保持黑色。

我已经尝试修改附加的代码,但只能使括号变成红色,而其余的文字留在黑色。 我想我需要添加正则expression式范围\[.*?\]\<.*?\>但不知道如何。 请帮忙!

 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 

Len(y) (当y包含单个字符时)将始终返回值1。

正确的长度是string之间存在xy存在于string之间的字符数,因此您需要使用如下所示的字符:

 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) 

为了迎合和<...>我的首选项将是修改子程序,以允许被search括号的types作为parameter passing,然后调用子程序两次。

 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 

迭代,并允许单个单元格内的多个括号的实例:

 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