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
'莱恩(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
@ YowE3K如果我设置为: 'With Found.Characters(Start:= InStr(Found.Text,x),Length:= InStr(Found.Txt,y) - InStr(Found.Txt,x)+ 1)' 根据您的指示。有任何想法吗?这个行上的 – Ilia
: 'With Found.Characters(Start:= InStr(Found.Text,x),Length:= Len(y))' 'Length:='value is 1. you need it to the字符数量之间的距离变为红色。 – MakPo