【问题标题】:Why cannot I ReDim an array with index of LongPtr or LongLong in Excel 2016 VBA为什么我不能在 Excel 2016 VBA 中 ReDim 索引为 LongPtr 或 LongLong 的数组
【发布时间】:2018-03-03 14:12:15
【问题描述】:

我习惯在 Excel 2010(Windows 7 64 位)上使用以下代码,效果很好。

Sub code_on_2010()
  Dim i As Long

  i = InputBox("input integer number")
  ReDim a(i) As Variant
  '....
End Sub

最近,我将我的电脑升级到 Windows 10(64 位)和 Excel 2016(64 位)。由于我知道 64 位长整数类型的新类型名称,我将代码重写如下:

Sub code_on_2016_with_LongPtr()
  Dim i As LongPtr

  i = InputBox("input integer number")
  ReDim a(i) As Variant
  '...
End Sub

它返回一个Type mismatch (Error 13) 错误。

即使我将 LongPtr 替换为 LongLong(如下所示),它也会返回 Type mismatch 错误。

Sub code_on_2016_with_LongLong()
  Dim i As LongLong

  i = InputBox("input integer number")
  ReDim a(i) As Variant
  '...
End Sub

谁能告诉我为什么我不能在 Excel 2016 VBA 中 ReDim 索引为 LongPtr 或 LongLong 类型的数组?

【问题讨论】:

  • 您的第一个和第二个代码在我的系统上运行良好....Windows 10 ....Excel 365 版本 1802 Build 9029.2167
  • 我不认为你可以分配那么多内存.. 我在Dim a(2 ^ 27) As Variant 附近收到Out of Memory 错误
  • @Gary'sStudent,微软是否只对 Excel 365 而不是 Excel 2016 做了一些改进?我的版本也是 1802(内部版本 9029.2167)。 :(
  • @PaichengWu 我知道没有区别......看我的回答............
  • 你为什么使用 LongPtr?这通常只对内存位置是必需的。

标签: excel vba


【解决方案1】:

Excel 64 位代码中不需要 LongPtr 或 LongLong:

Option Explicit

Sub code_on_2010()
Dim i As Long 'declaring any other type won't speed up your code, and won't give a bigger range of possible numbers!
Dim h$
Dim a() 'basically says : a is a variable sized array of type variant
h = InputBox("input integer number")  'returns a string
if isnumeric(h) then i=clng(h)
ReDim a(i)
'....
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 2012-05-21
    • 2015-10-31
    相关资源
    最近更新 更多