Cross Compatibility of Millisecond Timer Resolution in Excel VBA

自古美人都是妖i 提交于 2019-12-13 06:07:31

问题


I would like to get fractional timer resolution in an Excel VBA project that has to be compatible with Windows Excel 2007 and Mac OS Excel 2011. Ive found a method in windows utilizing sleep from kernel32.dll and a method on Mac OS using MacScript("GetMilliSec").

I currently have limited access to a Mac, so I am not able to test this out directly right now. If I declare Sleep like this:

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

Will an error be raised when the file is opened in a Mac? Or will the error be flagged only when/if Sleep is called in a Mac?


回答1:


I believe you'll need to use conditional compilation to avoid your API declaration causing an error on a Mac.

   #If Mac Then
      MsgBox "I'm a Mac"
   #Else
      MsgBox "I'm a PC"
   #End If



回答2:


I have a Mac using Office 2011 and can confirm that an error will be raised when the sub calls sleep with File not found: kernel32

If you have an alternative approach to use the sleep command in Mac, please share and use the below logic to select Mac or Windows:

Public Sub WINorMAC()
'Test for the operating system.
    If Not Application.OperatingSystem Like "*Mac*" Then
        'Is Windows.
        Call Windows_specific_function()
    Else
        'Is a Mac and will test if running Excel 2011 or higher.
        If Val(Application.Version) > 14 Then
            Call Mac_specific_function()
        End If
    End If
End Sub

You can also use this code to set a timer with up to 7 decimals, which works for both Mac and Windows:

'---------TIMER MACRO--------'
'PURPOSE: Determine how many seconds it took for code to completely run
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim StartTime As Double
Dim SecondsElapsed As Double
Dim minutesElapsed As Double

'Remember time when macro starts
StartTime = Timer

'*************************************
'------Start to run code--------'
'*************************************

        Your code goes here


'*************************************
'----------End code----------'
'*************************************


'Determine how many seconds code took to run

'If you want in seconds with two decimals, use this line:
'SecondsElapsed = Round(Timer - StartTime, 2)

'If you want with up to 7 decimals, use this line:
SecondsElapsed = Timer - StartTime

'Notify user how long the macro took
If SecondsElapsed > 60 Then
    minutesElapsed = Int(SecondsElapsed / 60)
    SecondsElapsed = Int(SecondsElapsed - (minutesElapsed * 60))

    Msgbox "This code ran successfully in " & minutesElapsed & " minutes and " & SecondsElapsed & " seconds", vbInformation

Else
    Msgbox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation

End If


来源:https://stackoverflow.com/questions/7246526/cross-compatibility-of-millisecond-timer-resolution-in-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!