Excel VBA单元格内容更变修改此单元格字体、颜色、底色?

C3:H7内单元格默认填充内容为【无】,黑色字体无底色,当某个格子内【无】修改成【1#设备故障】的时候,【1#设备故障】字体加粗变成红色,同时单元格底色填充其他颜色,没有修改的不变

请用鼠标右键单击工作表标签,选择“查看代码”,下拉“通用”下选择"Worksheet",下拉"声明"选择“Change",在代码框中插入下列代码:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column > 2 And Target.Column < 9 And Target.Row > 2 And Target.Row < 9 Then

        Target.Select

        If Target = "1#设备故障" Then

        With Selection.Font

            .Color = -16776961

            .TintAndShade = 0

        End With

        With Selection.Interior

            .Pattern = xlSolid

            .PatternColorIndex = xlAutomatic

            .Color = 65535

            .TintAndShade = 0

            .PatternTintAndShade = 0

        End With

        Selection.Font.Bold = True

    Else

       Target.Select

        With Selection.Font

            .ColorIndex = xlAutomatic

            .TintAndShade = 0

        End With

        With Selection.Interior

            .Pattern = xlNone

            .TintAndShade = 0

            .PatternTintAndShade = 0

        End With

        Selection.Font.Bold = False

    End If

End If

End Sub

保存为启用宏的工作簿 格式即可实现你的要求。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-09-07

选中C3:H7,开始,条件格式,新建规则,使用公式确定要设置格式的单元格

输入公式

=C3="1#设备故障"

再点右下角的格式,将字体修改为红色,加粗。然后在填充那里,选一种颜色,然后确定,确定。

第2个回答  2021-09-07

这种小事用不到VBA,用条件格式就可以做到

本回答被提问者采纳