Forum Discussion

lobo114's avatar
lobo114
Brass Contributor
Dec 04, 2024
Solved

Using VBA to make a cell blink

I am trying to figure out how to use VBA to make a cell blink in Excel. In the attached example, if a cell in column G is over 45 days old from the date in column F, I would like the corresponding cell in column D to blink between red and white for the fill color and leave the font color white. If possible, I would like the cell to blink once a second. Anybody have any ideas?

12 Replies

  • How about this:

     

    Dim NextBlink As Double
    
    Sub StartBlink()
        NextBlink = Now + TimeValue("00:00:01")
        Application.OnTime NextBlink, "BlinkCell"
    End Sub
    
    Sub BlinkCell()
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name
    
        Dim cell As Range
        For Each cell In ws.Range("G2:G" & ws.Cells(ws.Rows.Count, "G").End(xlUp).Row)
            If cell.Value <> "" And DateDiff("d", cell.Value, Now) > 45 Then
                With ws.Cells(cell.Row, "D")
                    If .Interior.Color = RGB(255, 255, 255) Then
                        .Interior.Color = RGB(255, 0, 0)
                    Else
                        .Interior.Color = RGB(255, 255, 255)
                    End If
                    .Font.Color = RGB(255, 255, 255)
                End With
            End If
        Next cell
    
        StartBlink
    End Sub
    
    Sub StopBlink()
        On Error Resume Next
        Application.OnTime NextBlink, "BlinkCell", , False
    End Sub

     

  • Also, keep in mind that blinking may be dangerous to users who are susceptible to epileptic seizures.

    And it becomes annoying very quickly.

    Finally: the existing conditional formatting overrules colors you set using VBA code.

    • lobo114's avatar
      lobo114
      Brass Contributor

      Ok, I didn't realize that the conditional formatting would overrule that. I could have it change the fill color of column C instead since there is no conditional formatting there. Here is what I was working on for the VBA code, but I messed it up somewhere.

      Sub BlinkCell()
      Dim CellToBlink As Range
      Set CellToBlink = Range("C8")
      Do While Range("G8").Value >= 45
          CellToBlink.Interior.ColorIndex = 3
          Application.Wait (Now + TimeValue("0:00:01"))
          CellToBlink.Interior.ColorIndex = 2
          Application.Wait (Now + TimeValue("0:00:01"))
          CellToBlink.Interior.ColorIndex = 3
          Application.Wait (Now + TimeValue("0:00:01"))
          DoEvents
      If Range("G8").Value < 45 Then Exit Do
      Loop

      If Range("G8").Value < 45 Then
      CellToBlink.Interior.ColorIndex = 2

      End Sub

       

  • mathetes's avatar
    mathetes
    Silver Contributor

    I know this isn't blinking but unless you give me a compelling reason why a bright red cell isn't obvious enough and you must have blinking I just don't want to encourage that usage. 

    This advice from m_tarler is very wise. I second it.

  • m_tarler's avatar
    m_tarler
    Bronze Contributor

    If you search for Excel VBA cell blink I'm sure you can find some code to do it.  It is that hard but I highly discourage it.  Excel has built in Conditional Formatting that would be much more efficient and will be compatible going forward.  VBA is not supported in web Excel or some other platforms and is disabled by default so anyone opening such a sheet has to open it in desktop version and allow the macros to run.

    In conditional formatting you can highlight column G and then choose a custom formula and simply type 

    =F1+45<G1

    and then choose bright red or something very obvious.

    I know this isn't blinking but unless you give me a compelling reason why a bright red cell isn't obvious enough and you must have blinking I just don't want to encourage that usage.  Remember if someone opens the sheet and does NOT allow macros then they will not see that blinking but with conditional formatting supported across platforms they will see the bright red cells.

    • lobo114's avatar
      lobo114
      Brass Contributor

      I have conditional formatting set to change the cell to red when "No" is selected. I want it to blink when it has been "No" for over 45 days.

      • m_tarler's avatar
        m_tarler
        Bronze Contributor

        So use Hot Purple/Pink instead?  Or change the Red to Orange and over 45 to Red.  But I still think blinking / VBA is not the best solution.  Remember you can do MORE than just fill using conditional formatting so you could also change font bold and underline, you can add patterns to the fill and you can add borders:

         

Resources