2017-03-01 174 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 
相關問題