2017-03-01 109 views
0

我需要将[方括号]中的所有内容都包含在这些<brackets>中的所有HTML/XML标记中。单元格中其余的文本需要保持黑色。在Excel单元格中对部分文本进行着色

我已尝试修改附加的代码,但只能将括号转为红色,而文本的其余部分留在黑色中。我想我需要添加正则表达式范围\[.*?\]\<.*?\>,但不知道如何。请帮忙!

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 
+0

'莱恩(Y)'应可能是'INSTR(Found.Txt,Y ) - Instr(Found.Txt,x)+ 1'(或者,如果你不想让'['和']'自己变成红色,使用'Start:= InStr(Found.Text,x)+ 1,Length:= Instr(Found.Txt,y) - Instr(Found.Txt,x) - 1') – YowE3K

+0

@ YowE3K如果我设置为: 'With Found.Characters(Start:= InStr(Found.Text,x),Length:= InStr(Found.Txt,y) - InStr(Found.Txt,x)+ 1)' 根据您的指示。有任何想法吗?这个行上的 – Ilia

+0

: 'With Found.Characters(Start:= InStr(Found.Text,x),Length:= Len(y))' 'Length:='value is 1. you need it to the字符数量之间的距离变为红色。 – MakPo

回答

1

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

一个值的正确长度你后是一个字符,其中存在字符串中x并且其中y存在之间的数在字符串中,所以你需要使用类似:

With Found.Characters(Start:=InStr(Found.Text, x), _ 
         Length:=Instr(Found.Text, y) - Instr(Found.Text, x) + 1) 

,或者,如果你想不色的括号本身,你可以添加1到开始POSI灰和来自长度减去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 

迭代,并允许在单个细胞内支架的多个实例:

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 
+0

这就像一个魅力,谢谢! 对于变量,例如HTML/XML标记和{}',是否可以包含'<>'?正则表达式'或'进入'x =“[| <| {” y =“] |> |}”'字符串?不知道如何在这里添加它们,而不是创建几个模块... – Ilia

+0

@伊利亚尔 - 你的最后一条评论因为(我认为)'<'被截断了,但我已经猜出了你要问什么,已经相应地编辑了答案。 – YowE3K

+0

@Ilia - 使用正则表达式可以让你一次处理所有的括号类型,但是很难一次性“查找”它们。您可能必须处理所有单元格,而不是仅查找“Find”所在的单元格,而且我认为多次使用Find可以更有效,而不是沿着这条路线走。 – YowE3K

0
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 
相关问题