我在excel中有一张带有数据的表格。我希望只要更新任何单元格中的任何数据,该单元格就会变色。由于没有条件(除了更新单元格之外),因此无法使用条件格式进行设置。这可以通过VBA完成。
答案 0 :(得分:1)
右键单击工作表标签,然后选择“查看代码”。
然后输入以下内容:
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范围内的任何单元格,在这种情况下,颜色将变为红色。 来源:these MS docs与this组合。
答案 1 :(得分:-1)
尝试.Interior.ColorIndex
.Cells(i, "A")`.Interior.ColorIndex` = 6
i
是行号,"A"
是列名。 6
=黄色
因此,本示例将为您的单元格涂上黄色
可重现的示例:此示例将在找到字符串“ test”时将单元格着色为黄色
Sub calculateamlp()
Dim count As Long, i As Long, j As Long, rw As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
With ws
rw = .Range("A" & .Rows.count).End(xlUp).Row
For i = 1 To rw
For j = 1 To Len(.Cells(i, "A").Value)
If InStr(j, .Cells(i, "A").Value, "test", vbTextCompare) Then
.Cells(i, "A").Interior.ColorIndex = 6
count = count + 1
j = InStr(j, .Cells(i, "A").Value, "test", vbTextCompare)
End If
Next j
Next
End With
End Sub
会给您: