使Long变量在64位和32位Excel VBA中工作 [英] Making Long variables work in 64 bit AND 32 bit Excel VBA
问题描述
我有一个相当复杂的Excel VBA项目,可以在32位系统上很好地运行,现在我也试图使其也适用于64位系统.我已经解决了所有函数声明,但是Long
数据类型仍然存在一些问题.
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.
做一些研究,我想出了这个(来自MSDN页面):
Doing some research, I come up with this (from the MSDN pages):
LongPtr 解析为的实际数据类型取决于其运行所在的Office版本: LongPtr 解析为32中的 Long 位版本的Office,而 LongPtr 解析为64位版本的Office中的 LongLong .
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.
但这是否意味着我可以更改所有Dim
语句,
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?
推荐答案
我同意@Ken White的回答.
备注(以获得一些答案):
I agree with @Ken White's answer.
Remark (for some answers):
- 您不要求将32位项目转换为64位项目(该项目在32位上不起作用).
- 您总是需要声明变量类型(如果您是一个优秀的程序员)
您仍然可以在Office 64位中使用Long数据类型.它将兼容在32位和64位Office中运行.
但是有时候,您没有选择的余地,因为某些对象的属性在32位和64位中不是相同的数据类型.
例如,如果您使用ADO:
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
在32位Office上很长 -
rstTest.RecordCount
是64位Office上的LongLong rstTest.RecordCount
is Long on 32-bit OfficerstTest.RecordCount
is LongLong on 64-bit Office
如果要将rstTest.RecordCount存储到lngCount变量中,则必须使用LongPtr声明此变量,否则它将不适用于32位和64位:
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
这篇关于使Long变量在64位和32位Excel VBA中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!