首页 > 解决方案 > 在 Excel 中使单元格闪烁(宏中的宏)

问题描述

如果 X1 中的值高于 999,我想让一个单元格“Z1”闪烁。我制作了 2 个模块,一个用于在值高于 999 时发出警报,Z1 有一个计数器来检查值高于 999 的次数。现在,每当计数器增加或 X1 值 > 999 时,我需要 Z1 闪烁并恢复到其原始状态。只卡在不闪烁的部分。

下面是我的代码,我哪里错了。

Private BlnAboveHundred1K As Boolean
Private IntCounter1K As Integer

Function TargetCounterROW1COL1(RngMeasure As Range)
    
   If RngMeasure.Value > 999 Then
        If BlnAboveHundred1K = False Then
            IntCounter1K = IntCounter1K + 1
           
            If Sheets("WorkArea").CommandButton14.Caption = "On" Then
            Call PlaySound("c:\windows\media\Alert4.wav", _
            0, SND_ASYNC Or SND_FILENAME)

            Call StartBlink

            End If
            
        BlnAboveHundred1K = True
        Else
        BlnAboveHundred1K = True
        'IntCounter = 0
    End If
Else
    BlnAboveHundred1K = False
End If

TargetCounterROW1COL1 = IntCounter1K

End Function

下面的单元格闪烁模块

Private IntBlinkCounter As Integer
Sub StartBlink()

Dim xCell As Range

Dim xTime As Variant
On Error Resume Next
Set xCell = Range("WorkArea!Z11")
On Error Resume Next

If xCell.Font.Color = vbRed Then
xCell.Font.Color = vbWhite
Else
xCell.Font.Color = vbRed

End If
xTime = Now + TimeSerial(0, 0, 1)

IntBlinkCounter = IntBlinkCounter + 1
If IntBlinkCounter < 5 Then
Application.OnTime xTime, "'" & ThisWorkbook.Name & "'!StartBlink", , True
End If

End Sub

标签: excelvba

解决方案


我注释掉了一些行,现在闪烁的宏对我有用:

Sub StartBlink()
  Dim xCell As Range
  'Dim i As Integer
  Dim xTime As Variant
  'On Error Resume Next
  Set xCell = Range("WorkArea!Z1")
  'On Error Resume Next
  If xCell.Font.Color = vbRed Then
      xCell.Font.Color = vbWhite
  Else
      xCell.Font.Color = vbRed
  End If
  xTime = Now + TimeSerial(0, 0, 1)
  'i = i + 1
  Application.OnTime xTime, "'" & ThisWorkbook.Name & "'!StartBlink", , True
  'If i = 6 Then i = 0
  'Application.OnTime xTime, "'" & ThisWorkbook.Name & "'!StartBlink", , False
End Sub

此行将宏添加到计划中:
Application.OnTime xTime, "'" & ThisWorkbook.Name & "'!StartBlink", , True
而另一行 从计划中删除宏:
Application.OnTime xTime, "'" & ThisWorkbook.Name & "'!StartBlink", , False
基本上,您添加它并立即将其删除。这就是它从未真正开始的原因。


如果它没有帮助,请尝试更改Call StartBlink为类似

Application.OnTime Now + TimeSerial(0, 0, 1), "'" & ThisWorkbook.Name & "'!StartBlink", , True

为了在几次眨眼后停止眨眼,你需要两个 thigs。

首先,在闪烁宏之外,在模块的最顶部,创建一个闪烁计数变量,例如:
Private IntBlinkCounter As Integer

二,在StartBlink宏里面,
Application.OnTime xTime, "'" & ThisWorkbook.Name & "'!StartBlink", , True

IntBlinkCounter = IntBlinkCounter + 1
If IntBlinkCounter < 5 Then
    Application.OnTime xTime, "'" & ThisWorkbook.Name & "'!StartBlink", , True
End If

推荐阅读