在VBA中闪烁单元格

时间:2016-10-13 12:58:13

标签: excel vba excel-vba

我有一些代码,当一个单元格具有一定值时,它将其内部变为红色,其字体变为白色。我想要做的是使文本的颜色每秒在白色和红色之间交替,只要细胞内部为红色(一旦它变成红色,它将保持红色)。 我希望用户能够感觉到单元格实际上在闪烁。

我写了这段代码:

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只是制作了第一个具有红色内饰的单元格" flash"然后崩溃。红细胞不是连续的。

1 个答案:

答案 0 :(得分:2)

快来:

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