首页 > 解决方案 > excel中的单元格颜色

问题描述

我在excel中有一个数据表。我希望一旦任何单元格中的任何数据更新,单元格就会被着色。由于没有条件(除了单元格已更新),我无法使用条件格式来做到这一点。这可以通过 VBA 完成。

标签: excelvba

解决方案


右键单击工作表选项卡,然后选择“查看代码”。

然后输入以下内容:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim KeyCells As Range
    Dim CellIntersect As Range

    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("A1:C10")

    Set CellIntersect = Application.Intersect(KeyCells, Target)

    If Not CellIntersect Is Nothing Then
        ' Change background color to red
        CellIntersect.Interior.Color = RGB(255, 0, 0)

    End If
    End Sub

如果您更改 KeyCells 范围内的任何单元格,在这种情况下颜色将变为红色。资料来源:这些 MS 文档this相结合。


推荐阅读