我有一个电子表格,我想要A:自动格式化的范围,以便字符显示为红色,数字保持相同的颜色。以下宏似乎工作正常,但我每次更改单元格中的值时都需要手动运行它:
Sub Red_text()
Dim i As Integer
Dim MyString As String
MyString = ActiveCell.Value
For i = 1 To Len(MyString)
If IsNumeric(Mid(MyString, i, 1)) = False Then
ActiveCell.Characters(i, 1).Font.Color = RGB(247, 66, 66)
End If
Next i
End Sub
所以基本上我需要将它改成一个事件宏,每次编辑它时都会重新格式化当前单元格。并将此行为限制为A:范围。
非常感谢任何帮助!!
答案 0 :(得分:1)
首先对您的宏稍作修改:
Sub Red_text(r As Range)
Dim i As Integer
Dim MyString As String
MyString = r.Value
For i = 1 To Len(MyString)
If IsNumeric(Mid(MyString, i, 1)) = False Then
r.Characters(i, 1).Font.Color = RGB(247, 66, 66)
End If
Next i
End Sub
并在工作表代码区域中包含以下事件宏:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range
Set A = Range("A:A")
If Intersect(A, Target) Is Nothing Then Exit Sub
Application.EnableEvents = False
Call Red_text(Target)
Application.EnableEvents = True
End Sub
事件宏检测到A列的条目,然后应用格式。
修改#1 强>:
事件宏必须更改为一次处理多个单元格。删除原始事件宏并使用此:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, rBIG As Range, r As Range
Set A = Range("A:A")
Set rBIG = Intersect(A, Target)
If rBIG Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In rBIG
Call Red_text(r)
Next r
Application.EnableEvents = True
End Sub