我在 excel 中有大量包含电话和手机号码的数据。请告诉我如何为电话代码赋予特定颜色(例如红色)。例如 022-1234567 或 +91-1234567890。请告诉我如何在 excel 中为整个列或单个单元格仅添加 +91 和 022 的颜色。
Sub Macro1()
ActiveCell.FormulaR1C1 = ActiveCell.Value
With ActiveCell
.Characters(Start:=1, Length:=0)
.Font
.Name = "Arial"
.ThemeFont = xlThemeFontNone
End With
With ActiveCell
.Characters(Start:=1, Length:=3)
.Font
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.Color = -16776961
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With End Sub
答案1
如果我们从以下开始:
并运行这个简短的宏:
Sub ColorMeRed()
Dim r As Range, rng As Range, v As String
Dim i As Long
Set rng = Range("B1:B10")
For Each r In rng
v = r.Text
i = InStr(v, "-")
If i > 0 Then
r.Characters(Start:=1, Length:=i - 1).Font.Color = vbRed
End If
Next r
End Sub
我们最终得到: