【问题标题】:Flashing cells in VBAVBA中的闪烁单元格
【发布时间】:2016-10-13 21:37:17
【问题描述】:

我有一些代码,当单元格具有特定值时,它会将其内部更改为红色,并将其字体更改为白色。我想要做的是让文本的颜色每秒在白色和红色之间交替,只要单元格内部是红色的(一旦变成红色,它将保持红色)。 我希望用户有这样的印象,即单元格实际上是在闪烁。

我写了这段代码:

For r = 6 To 1000
    With .Cells(r, 6)
        While .Interior.Color = RGB(237, 67, 55)
            .Font.Color = RGB(237, 67, 55)
             Application.Wait (Now + TimeValue("0:00:01"))
            .Font.Color = vbWhite
        Wend
    End With
Next r

excel 只是使第一个具有红色内部“闪光”的单元格然后崩溃。红细胞的顺序不连续。

【问题讨论】:

  • 请参阅here,您可以稍微调整一下以满足您的需求,因为它可以让我将其粘贴到新模块中。
  • 您可以使用条件格式,其中条件取决于单元格的值和隐藏的固定单元格的值(甚至可能是命名值)。只需每秒切换一次此固定单元格的值,让条件格式发挥其魔力。

标签: vba excel


【解决方案1】:

试一试:

Sub Flash_Ahhh()

Dim strRange As String
Dim rCell As Range
Dim iFlasher As Integer

lngCounter = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row    'Find last row of data
lngCol = ActiveCell.Column                                          ' Find the active column

vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)                                                'The Active Column Letter

strRange = Col_Letter & "6:" & Col_Letter & lngCounter              'The range of all cells in the active column



    For Each rCell In Range(strRange).Cells


            Select Case rCell.Interior.Color

                Case Is = vbRed

                    For iFlasher = 1 To 10

                        If rCell.Font.Color = vbRed Then
                            rCell.Font.Color = vbWhite
                            Else
                            rCell.Font.Color = vbRed
                        End If

                        Call WaitFor(0.1)


                    Next iFlasher
                    rCell.Font.Color = vbWhite

                Case Else

            End Select



    Next rCell

End Sub

使用以下方法导致时间延迟:

Sub WaitFor(NumOfSeconds As Single)
    Dim SngSec As Single
    SngSec = Timer + NumOfSeconds

    Do While Timer < SngSec
        DoEvents
   Loop
End Sub

【讨论】:

  • 为什么不Call WaitFor(1) 闪一秒??
  • @Gary'sStudent 这是循环和延迟之间的权衡。您可以摆弄数字以更改“闪光度”和闪光长度。如果您从 6:1000 开始,您可能还想做一个 rCell.Select 来跳下该列
  • 好的........我投了赞成票,因为我认为WaitFor() 很酷。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多