每当发生满足特定条件的更改时,我都会使用以下类似代码的版本作为时间戳。我不知道为什么下面的代码不起作用。
[如果单元格字体颜色等于14(深蓝色),它将向右移动5列,并将单元格值更改为“ ROOF”。
Private Sub Worksheet_Colour(ByVal Target As Range)
Dim ptInt As Range
Dim rangeCell As Range
Dim sCell As Range
Dim cCell As Integer
Set ptInt = Intersect(Target, Range("D12:D70"))
If Not ptInt Is Nothing Then
For Each rangeCell In ptInt
cCell = rangeCell.Font.ColorIndex
If cCell = 14 Then
Set sCell = rangeCell.Offset(0, 5)
sCell.Value = "ROOF"
End If
Next
End If
End Sub
编辑:
我设法使代码执行我想要的操作,但是,仅当更改单元格值时才触发,是否可以在更改字体时触发它?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rInt As Range
Dim rCell As Range
Dim tCell As Range
Set rInt = Intersect(Target, Range("G12:G116"))
If Not rInt Is Nothing Then
For Each rCell In rInt
If Target.Font.ColorIndex = 23 Then
Set tCell = rCell.Offset(0, 7)
'If IsEmpty(tCell) Then
tCell = "ROOF"
'End If
End If
Next
End If
End Sub
固定代码为:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ptInt As Range
Dim rangeCell As Range
Dim sCell As Range
Dim cCell As Integer
Set ptInt = Intersect(Target, Range("D12:D70"))
If Not ptInt Is Nothing Then
For Each rangeCell In ptInt
cCell = rangeCell.Interior.ColorIndex
If cCell = 49 Then
Set sCell = rangeCell.Offset(0, 5)
sCell.Value = "ROOF"
End If
Next
End If
End Sub
问题出在rangeCell.Font.ColorIndex
行我将其替换为rangeCell.Interior.ColorIndex
行,使深蓝色值达到49。
要使用不同的颜色查看不同的值,请使用MsgBox(rangeCell.Interior.ColorIndex)
。