Sub ColorRed() Dim rng As Range Dim sp As Integer, ep As Integer, le As Integer For Each rng In Selection If rng.Value <> "" Then sp = IIf(InStr(1, rng.Value, "[") > 0, InStr(1, rng.Value, "["), InStr(1, rng.Value, "[")) ep = IIf(InStr(1, rng.Value, "]") > 0, InStr(1, rng.Value, "]"), InStr(1, rng.Value, "]")) le = ep - sp + 1 If sp > 0 And le > 0 Then rng.Characters(sp, le).Font.Color = vbRed End If End If Next rng End Sub 上文文代码可以实现第一个中括号中内容为红色,怎么循环应用(在一个格子中出现复数的中括号)
最新回答
月光很浅思念很暖
2024-10-18 07:35:55
Sub ColorRed() Dim rng As Range Dim sp As Integer, ep As Integer, le As Integer For Each rng In Selection If rng.Value <> "" Then ep = 1: rng.Characters.Font.Color = vbBlack Do sp = InStr(ep, rng.Value, "[") If sp = 0 Then Exit Do ep = InStr(sp, rng.Value, "]") sp = InStrRev(rng.Value, "[", ep) le = ep - sp + 1 If sp > 0 And le > 0 Then rng.Characters(sp, le).Font.Color = vbRed Else Exit Do End If Loop End If Next rng End Sub