使长变量在 64 位和 32 位 Excel VBA 中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24095500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making Long variables work in 64 bit AND 32 bit Excel VBA
提问by M. H
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.
我有一个相当复杂的 Excel VBA 项目,它在 32 位系统上运行良好,我现在正试图让它也适用于 64 位系统。我已经解决了所有的函数声明,但仍然有一些Long
数据类型的问题。
Doing some research, I come up with this (from the MSDN pages):
做了一些研究,我想出了这个(来自 MSDN 页面):
The actual data type that LongPtrresolves to depends on the version of Office that it is running in: LongPtrresolves to Longin 32-bit versions of Office, and LongPtrresolves to LongLongin 64-bit versions of Office.
LongPtr解析为的实际数据类型取决于运行它的 Office 版本:LongPtr在 32 位版本的 Office 中解析为Long,而LongPtr在 64 位版本的 Office 中解析为LongLong。
But does that mean that I can change ALL my Dim
statements from
但这是否意味着我可以改变我所有的Dim
陈述
Dim x as Long
to
到
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?
我应该到处都这样做吗?还是我拿错了棍子的一端?是否存在这种方法不起作用或应避免这种更改的情况?
回答by Sly
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)
我同意@Ken White 的回答。
备注(对于某些答案):
- 您不会要求将 32 位项目转换为 64 位项目(这不适用于 32 位)。
- 你总是需要声明变量类型(如果你是一个优秀的程序员)
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:
您仍然可以在 Office 64 位中使用 Long 数据类型。它将兼容在 32 位和 64 位 Office 中运行。
但有时,您没有选择,因为某些对象的属性在 32 位与 64 位中的数据类型不同。例如,如果您使用 ADO:
Dim rstTest As ADODB.Recordset
rstTest.RecordCount
is Long on 32-bit OfficerstTest.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:
rstTest.RecordCount
在 32 位 Office 上为 Long 在rstTest.RecordCount
64位 Office 上为 LongLong
如果要将 rstTest.RecordCount 存储到 lngCount 变量中,则必须使用 LongPtr 声明此变量,否则它将不适用于 32 位和 64 位:
Dim lngCount As LongPtr
lngCount = rstTest.RecordCount
回答by MatthewHagemann
Try LongLong. From the spec, it looks like LongLong is the replacement for the 32 bit type Long.
试试龙龙。从规范来看,LongLong 似乎是 32 位类型 Long 的替代品。