0

我想在必须与 Windows Excel 2007 和 Mac OS Excel 2011 兼容的 Excel VBA 项目中获得小数计时器分辨率。我在 windows 中找到了一种使用sleep fromkernel32.dll的方法和在 Mac OS 上使用MacScript("GetMilliSec").

我目前对 Mac 的访问权限有限,因此我现在无法直接对其进行测试。如果我Sleep这样声明:

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

在 Mac 中打开文件时会出现错误吗?Sleep还是仅当/如果在 Mac 中调用时才会标记错误?

4

2 回答 2

2

我相信您需要使用条件编译来避免您的 API 声明在 Mac 上导致错误。

   #If Mac Then
      MsgBox "I'm a Mac"
   #Else
      MsgBox "I'm a PC"
   #End If
于 2011-08-30T16:58:02.200 回答
1

我有一台使用 Office 2011 的 Mac,并且可以确认当 sub 调用时会引发sleep错误File not found: kernel32

如果您有其他方法sleep在 Mac 中使用该命令,请分享并使用以下逻辑选择 Mac 或 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

您还可以使用此代码设置最多 7 位小数的计时器,适用于 Mac 和 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
于 2016-02-16T13:46:34.753 回答