Test Platforms

Although Excel runs on both PCs and Macs, there are quite a few differences between the objects Excel provides for each of those operating systems. Also, code written for the PC often relies on external components that are not available on the Mac or on early versions of Windows. In other words, it is very difficult to get the same code to work correctly on multiple operating systems.

The easiest solution to this problem is to require a specific operating system. The second easiest solution is to pick one operating system as your primary target and provide a reduced feature set on the others.

You can tell which operating system is in use by checking Application.OperatingSystem. The following code checks the operating system when the workbook loads and warns the user if it is not the primary target:

' ThisWorkbook class Private Sub Workbook_Open( ) Select Case GetOS Case OS.Win32 ' Full features, no message. Case OS.Mac ' Reduced features, display a warning. MsgBox "Running in compatibilty mode. "& _ "Some features are disabled.", vbExclamation Case OS.Win16 ' Not supported at all! MsgBox "This application requires Windows NT or XP.", vbCritical Application.Quit End Select End Sub   ' Platform module Enum OS Win16 Win32 Mac End Enum   Function GetOS( ) As OS Dim result As OS Dim s As String s = Application.OperatingSystem If InStr(1, s, "Windows") Then If InStr(1, s, "32-bit") Then result = Win32 Else result = Win16 End If Else result = Mac End If GetOS = result ...

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.