2011-08-30 55 views
0

我想在必须与Windows Excel 2007和Mac OS Excel 2011兼容的Excel VBA项目中获得小数计时器分辨率。我在窗口中使用来自kernel32.dll以及使用MacScript("GetMilliSec")的Mac OS上的方法。Excel VBA中的毫秒计时分辨率的交叉兼容

我目前对Mac有限制,所以我现在无法直接测试。如果我宣布Sleep这样的:

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

将当文件在Mac上打开一个错误得到提升?或者只有在/在Mac中调用Sleep时才会标记错误?

回答

1

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

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

我以前见过这个代码...是'Mac'的东西,这将是在条件汇编“代码”中承认?对我来说,仅向Mac用户分发第二个版本的项目是不可行的,所以我不能使用'#CONST Mac = True',然后用'#CONST Mac = False'手动更改并重新分配 –

+0

@Hari - 我以为有一些“内置的”条件编译常量,但似乎并非如此。我测试了我的代码(没有声明“Mac”常量),它似乎工作,但现在我发现即使使用未声明的常量也不会出错 - 它只是给它一个默认值False。 –

+0

@Tim ...感谢您的努力!这给了我我需要知道的东西。 –

2

我使用Office 2011 Mac上,可以确认的是,当子调用sleepFile not found: kernel32

如果你必须使用在Mac上sleep命令的替代方法,请共享一个错误将被上调并使用下面的逻辑来选择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 

您CA n您还可以使用此代码来设置多达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