-1

What issue I'm facing is, I want to distribute one of my Excel VBA Template. Almost in all procedure, I have assigned LONG datatype. but, I found that for VBA7 or 2010 and later version, we can use LongPtr datatype for assigning. which will bypass the versioning issue. whether it will be a 64bit or 32bit of MS office.

But one my mate has pointed out I can't use LongPtr for VB6 or for lower of 2010 office.

So I have tried to use in the way to make it more usable for any office version

#If VBA7 Then
    Dim X As LongPtr
#Else
    Dim X As Long
#End If

Should I use above declaration? Will it cause the error? According to my friend, It will cause. He also added that compiler will bypass the if statement.

I don't have a system to test it.

My current system is,

Windows 10 64bit

MS Office 2016 32bit

Please suggest me, where I'm missing the concept. And Why I can't use the above declaration in procedure?

Thank you.

Community
  • 1
  • 1
Kamal Bharakhda
  • 129
  • 1
  • 12

1 Answers1

2
#If VBA7 Then
    Dim X As LongPtr
#Else
    Dim X As Long
#End If

Should I use above declaration? Will it cause the error?

Yes, you should use the above declaration, and it will not cause an error, even in an ancient version of Office.

However, you should understand when and why you need to use LongPtr instead of Long to begin with.
Long exists in all versions and means the same thing in all versions (32-bit integer). You should not change it to LongPtr for the sake of it. You should only use LongPtr for pointers or pointer-sized data types.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • are your sure pal, I can use declaration? because I haven't yet tested it but My friend is confidently said it will throw an error. and yes thank you for the quick reply. – Kamal Bharakhda Jun 06 '18 at 18:12
  • I think this is really nothing but a google question https://msdn.microsoft.com/en-us/VBA/Language-Reference-VBA/articles/longptr-data-type – Storax Jun 06 '18 at 18:14
  • 1
    @KamalBharakhda Yes, I am sure that you can use this declaration. That is precisely the reason why [conditional compilation](https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/understanding-conditional-compilation) exists. – GSerg Jun 06 '18 at 18:15
  • @Storax, pal, you have not understood my question properly. I have already googled this query and went through the same page, but they said, we can use LongPtr only on 2010 or after. Now, if I want to distribute my workbook to the clients, and if they have the earlier version where their system will not support the LongPtr data type then what? So, to make my application more portable I have placed here my query so I can find whether I'm going in the right direction or not? If you have downvote my question then please upvote it. thanks. – Kamal Bharakhda Jun 06 '18 at 19:54
  • I understood the question and you still don't google. The page I mentioned is a starting point to get to, for example, the following page https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/compiler-constants. And so on and on ... – Storax Jun 06 '18 at 20:04