Excel: Mac or Win?

On occasion you may need your instance of Excel to know whether it’s living in a Mac or Windows environment. Perhaps you have a VBA routine which checks page breaks? E.g.

ActiveWindow.View = xlPageBreakPreview

All well and good, except that function doesn’t work in Excel for Mac. Now if you’re only running in Excel for Mac then you’ll just have to take that piece of code out and live with the lack. But what if you want to run on either one or the other depending on whether you’re at home or work? Working on your laptop or desktop? Or just because?

Now this is the time to tweak that code, perhaps with the following:

If MacOrWin = False Then
ActiveWindow.View = xlPageBreakPreview
End If

Which is all well and good, provided we actually remember to write the code for that boolean “MacOrWin” value. Drop the following into the ThisWorkbook module and away we go:

Public MacOrWin As Boolean

Private Sub Workbook_Open()
MacOrWin = checkMac
End Sub

Function checkMac()
'Test the conditional compiler constant
#If Mac Then
'It's Excel for Mac, but which version?
If Val(Application.Version) >= 14 Then
'And it's Office for Mac 2011 or later!
checkMac = True
End If
'It's Excel for Windows
checkMac = False
#End If
End Function

Use the MacOrWin test for any functions that don’t carry into one or the other, provide the relevant If Else or Select Case scenario and all is well again:

If foo Then bar
End If