Sleep Equivalent for mac in Excel VBA

Viewed 308

I want to add a wait block for 500 milliseconds in macOS VBA editor for Excel. I know

Application.Wait (Now + TimeValue("0:00:01"))

Works, but this can't go under 1 second. I have tried the sleep command too:

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
...
Sleep 500 

But this gives me an error saying

File Not Found "kernel32" 

Any way to achieve this?

2 Answers

This will work on both Windows and Mac:

Option Explicit

#If Mac Then
    #If VBA7 Then
        Public Declare PtrSafe Sub USleep Lib "/usr/lib/libc.dylib" Alias "usleep" (ByVal dwMicroseconds As Long)
    #Else
        Public Declare Sub USleep Lib "/usr/lib/libc.dylib" Alias "usleep" (ByVal dwMicroseconds As Long)
    #End If
#Else 'Windows
    #If VBA7 Then
        Private Declare PtrSafe Sub MSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
    #Else
        Private Declare  Sub MSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
    #End If
#End If

Public Sub Sleep(ByVal dwMilliseconds As Long)
#If Mac Then
    USleep dwMilliseconds * 1000&
#Else
    MSleep dwMilliseconds
#End If
End Sub

Your code block above will work with one small addition. Simply divide the TimeValue by 2 to get 500ms

Application.Wait (Now + TimeValue("0:00:01") / 2)
Related