请教,如何在excel2007单元格里面设置输入特定的字变色?
发布网友
发布时间:2022-03-03 08:13
我来回答
共1个回答
热心网友
时间:2022-03-03 09:42
这得用到VBA才可以处理,知道VBA怎么用吗?
单元格事件完成
代码如下
Private Sub Worksheet_Change(ByVal Target As Range)
Dim str1$, x&, i&, str2$
str1 = Target.Value
For x = 1 To Len(str1)
str2 = Mid(str1, x, 1)
If str2 = "胜" Then
Target.Characters(Start:=x, Length:=0).Font.ColorIndex = 3
ElseIf str2 = "负" Then
Target.Characters(Start:=x, Length:=0).Font.ColorIndex = 14
ElseIf str2 = "平" Then
Target.Characters(Start:=x, Length:=0).Font.ColorIndex = 4
Else
Target.Characters(Start:=x, Length:=0).Font.ColorIndex = xlNone
End If
Next x
End Sub
以上是任意一个单元格输入都得到的效果
以下代码指定B列输入时才运行代码,你根据你的表格修改列号即可
Private Sub Worksheet_Change(ByVal Target As Range)
Dim str1$, x&, i&, str2$
If Target.Column <> 2 Then Exit Sub
If Target.Count <> 1 Then Exit Sub
str1 = Target.Value
For x = 1 To Len(str1)
str2 = Mid(str1, x, 1)
If str2 = "胜" Then
Target.Characters(Start:=x, Length:=0).Font.ColorIndex = 3
ElseIf str2 = "负" Then
Target.Characters(Start:=x, Length:=0).Font.ColorIndex = 14
ElseIf str2 = "平" Then
Target.Characters(Start:=x, Length:=0).Font.ColorIndex = 4
Else
Target.Characters(Start:=x, Length:=0).Font.ColorIndex = xlNone
End If
Next x
End Sub