2011-08-30 69 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