Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional compilation based on Excel version

I know that I can use conditional compilation in VBA to differentiate between the 64bit and the 32bit version (using #If VBA7 Then ...).

Is there also a build in constant similar to Application.Version so that I can differentiate between Excel 2013/2010/2007/... etc. at compile time?

Also, is there a list of available existing compiler constants? So far I found VBA7 and Win64 (e.g. from this article) - but are there any other ones?

like image 767
Peter Albert Avatar asked Jan 29 '13 10:01

Peter Albert


People also ask

Can you compile Excel VBA code?

You can't do it, plain and simple. VBA code lives in an Excel workbook and there is no way, that I know, to compile that code outside of a workbook. The long and the short of it is: The users of your Excel programs must have Excel installed.

What is conditional compilation in C?

Conditional compilation is the process of selecting which code to compile and which code to not compile similar to the #if / #else / #endif in C and C++. Any statement that is not compiled in still must be syntactically correct.

Can you do conditional formatting in VBA?

Conditional Formatting in Excel VBA. We can apply conditional formatting. It can be found in the styles section of the Home tab. read more to a cell or range of cells in Excel.


2 Answers

From this link, you have the following constants:

VBA6
VBA7
Win64
Win32
Win16
Mac
like image 51
iDevlop Avatar answered Oct 02 '22 21:10

iDevlop


Not to the version granularity questioned, but I had a need to separate Office 365/XL16- specific code from its predecessors. VBA7 is backwards compatible with VBA6, and VBA6 knows nothing of VBA7. We can use this to our advantage.

Sub testit()
#If VBA6 And VBA7 Then
   Debug.Print "Cool!" 'XL16 vba code
#Else
   Debug.Print "Yeah!" 'Not XL16 vba code
#End If
End Sub

XL14 and below evaluate #If as if true/false, XL16 as true/true. If it's just #If VBA7, XL14 attempts to compile the XL16 code. I don't know why, but that's how it tested.

like image 28
Mick536 Avatar answered Oct 02 '22 20:10

Mick536