Using Sleep function to pause code

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
708
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Using a simple "Sleep" function to toggle color of text in a cell.
Wanting to achieve 500 millisecond between alternations however, Sleep 500 is not giving me the half second pause and more like a 5 second total time.
I've changed the value from 500 all the way down to 0.5 and minimal change in alternating time. Best I can get is 4 second s to alternate 3 times.

Am I missing something?

VBA Code:
Option Explicit

#If VBA7 Then ' Excel 2010 or later
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
#Else ' Excel 2007 or earlier
Public Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)
#End If

Sub BlinkText()
  Application.ScreenUpdating = True
  
  ActiveSheet.Range("InspectionDate").Activate 'Named range X11:AD11
  
  Dim i As Integer
  For i = 0 To 2
      With Selection.Font
          .Color = vbRed
          .Bold = True
      End With
      Sleep 500

      With Selection.Font
          .Color = vbBlack
          .Bold = False
      End With

      Sleep 500
  Next
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
After a PC restart, it seems to be working correct.....🤔
 
Upvote 0

Forum statistics

Threads
1,215,106
Messages
6,123,124
Members
449,096
Latest member
provoking

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top