[英]If activecell interior color changes then
I want the following VBA code:我想要以下 VBA 代码:
If the activecell interior color changes to colorindex 44 then I want the cell five columns to the right to have the text "Done" and today's date.如果 activecell 内部颜色更改为 colorindex 44,那么我希望右侧五列的单元格具有文本“完成”和今天的日期。
I have tried the below but only works when I move the selected cell to the right.我已经尝试了以下但仅在我将所选单元格向右移动时才有效。 It also does not stop working when a cell color is NOT changed.
当单元格颜色未更改时,它也不会停止工作。
Private Sub Worksheet_SelectionChange (ByVal Target As Range)
If ActiveCell.Offset(0, -1).Interior.ColorIndex = 44 Then
ActiveCell.Offset(0, 4) = "Done" & Date
End If
End Sub
You must use Target
instead of ActiveCell
您必须使用
Target
而不是ActiveCell
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Interior.ColorIndex = 44 Then
Target.Offset(ColumnOffset:=4).Value = "Done " & Date
End If
End Sub
Note that this cannot check if the color was changed or not as there is no event for that.请注意,这无法检查颜色是否已更改,因为没有事件。 You can only test if the actual color index is 44. But to prevent overwriting the "change" date you can just test if the date already exists.
您只能测试实际颜色索引是否为 44。但是为了防止覆盖“更改”日期,您可以测试该日期是否已经存在。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Rows.Count + Target.Columns.Count > 2 Then Exit Sub 'exit if more than one cell is selected
If Target.Interior.ColorIndex = 44 And Not Left$(Target.Offset(ColumnOffset:=4).Value, 4) = "Done" Then
Target.Offset(ColumnOffset:=4).Value = "Done " & Date
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.