当单元格中的值更改时,Worksheet_Change代码未运行

时间:2016-11-28 22:52:16

标签: excel vba

每次C76中的值因单元格A70:D70的变化而增加时,单词"谢谢"应该将颜色变为黑色然后再变回白色,当C76的值= 0时,使用命令按钮删除A70中的值:D70后,单词"重置"应该改变颜色

我对以下代码做了一些小改动,这是Sam在https://stackoverflow.com/a/15435969/7155787提供的。在单元格C76中手动输入值时工作,但是,当我们使用Excel函数将单元格C76与单元格A70:D70链接到C76时,代码不起作用。

Option Explicit

Private Sub CommandButton1_Click()
Sheets("Sheet1").Range("A70:D70").Value = "0"
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim ThankU As Range
Dim VtRst As Range
Set VtRst = Range("F74")
Set ThankU = Range("F62")
Set KeyCells = Range("C76")

If Not Application.Intersect(KeyCells, Range(Target.Address)) _
  Is Nothing Then
    If Target.Value > Cells(Target.Column, 10).Value Then
        'flash Black
        VtRst.Font.ColorIndex = 2
        ThankU.Font.ColorIndex = 1
        Pause 1
        ThankU.Font.ColorIndex = 2
    ElseIf Target.Value = 0 Then
        'flash red
        VtRst.Font.ColorIndex = 3
    End If
    Cells(Target.Column, 10).Value = Target.Value
End If
End Sub

'Pauses execution without holding up main UI thread
Public Function Pause(NumberOfSeconds As Variant)
On Error GoTo Error_GoTo
Dim PauseTime As Variant
Dim Start As Variant

    PauseTime = NumberOfSeconds
    Start = Timer
    Do While Timer < Start + PauseTime
        DoEvents
    Loop

Add to dictionary:
    On Error GoTo 0
    Exit Function
Error_GoTo:
    Debug.Print Err.Number, Err.Description, Erl
    GoTo Exit_GoTo
End Function

0 个答案:

没有答案