Home » excel » Making Long variables work in 64 bit AND 32 bit Excel VBA

Making Long variables work in 64 bit AND 32 bit Excel VBA

Posted by: admin March 7, 2020 Leave a comment


I have quite a complex Excel VBA project that runs well on 32-bit systems, I am now trying to make it work for 64-bit systems too. I have resolved all the function declarations, but still get some issues with the Long data type.

Doing some research, I come up with this (from the MSDN pages):

The actual data type that LongPtr resolves to depends on the version of Office that it is running in: LongPtr resolves to Long in 32-bit versions of Office, and LongPtr resolves to LongLong in 64-bit versions of Office.

But does that mean that I can change ALL my Dim statements from

Dim x as Long


Dim x as LongPtr

Should I do that everywhere? Or have I got the wrong end of the stick? Are there circumstances where that won’t work or this change should be avoided?

How to&Answers:

I agree with @Ken White’s answer.
Remark (for some answers):
– You don’t ask to convert your 32-bit project into a 64-bit project (which will not work on 32-bit).
– You always NEED to declare the variable type (if you are a good programmer)

You can still use the Long data type in Office 64-bit. It will be compatible for running in 32-bit and 64-bit Office.
But sometimes, you don’t have the choice, because some object’s properties are not the same data type in 32-bit vs. 64-bit.
For instance, if you use ADO:

Dim rstTest As ADODB.Recordset 

rstTest.RecordCount is Long on 32-bit Office
rstTest.RecordCount is LongLong on 64-bit Office
If you want to store rstTest.RecordCount into a lngCount variable, you MUST declare this variable with LongPtr otherwise it won’t work on 32-bit AND 64-bit:

Dim lngCount As LongPtr  
lngCount = rstTest.RecordCount


Try LongLong. From the spec, it looks like LongLong is the replacement for the 32 bit type Long.