gpt4 book ai didi

variables - 使长变量在 64 位和 32 位 Excel VBA 中工作

转载 作者:行者123 更新时间:2023-12-01 23:48:51 25 4
gpt4 key购买 nike

我有一个相当复杂的 Excel VBA 项目,可以在 32 位系统上运行良好,我现在正在尝试使其也适用于 64 位系统。我已经解决了所有函数声明,但仍然遇到 Long 数据类型的一些问题。

做了一些研究,我得出了这个(来自 MSDN 页面):

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 语句

Dim x as Long

Dim x as LongPtr

我应该在任何地方都这样做吗?还是我搞错了方向?是否存在这种情况不起作用或应该避免这种更改的情况?

最佳答案

我同意@Ken White的回答。
备注(一些答案):

  • 您不会要求将 32 位项目转换为 64 位项目(这不适用于 32 位)。
  • 你总是需要声明变量类型(如果你是一个优秀的程序员)

您仍然可以在 Office 64 位中使用 Long 数据类型。它将兼容在 32 位和 64 位 Office 中运行。
但有时,您别无选择,因为某些对象的属性在 32 位和 64 位中的数据类型不同。例如,如果您使用 ADO:

Dim rstTest As ADODB.Recordset 
  • rstTest.RecordCount 在 32 位 Office 上为 Long
  • rstTest.RecordCount 在 64 位 Office 上为 LongLong

如果要将 rstTest.RecordCount 存储到 lngCount 变量中,则必须使用 LongPtr 声明此变量,否则它将无法在 32 位和 64 位上运行:

Dim lngCount As LongPtr  
lngCount = rstTest.RecordCount

关于variables - 使长变量在 64 位和 32 位 Excel VBA 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24095500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com