Search code examples
excelvbamacroshidebasic

Why excel is not hiding my shape?


I'm using this code below...

Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)
Sub blink()
    Sleep 500
    ActiveSheet.Shapes("the_shape").Visible = False
    Sleep 500
    ActiveSheet.Shapes("the_shape").Visible = True
End Sub

am I missing something?


Solution

  • That is pretty strange. Adding DoEvents resolved the problem. I'm guessing that Sleep is pausing the thread before Excel has a chance to hide the shape.

    Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)
    
    Sub blink()
        Sleep 500
        ActiveSheet.Shapes("the_shape").Visible = False
        DoEvents
        Sleep 500
        ActiveSheet.Shapes("the_shape").Visible = True
    End Sub