我习惯在 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
Run Code Online (Sandbox Code Playgroud)
最近,我将电脑升级到 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
Run Code Online (Sandbox Code Playgroud)
它返回一个Type mismatch (Error 13)错误。
即使我用 LongLong 替换 LongPtr (如下所示),它Type mismatch也会返回错误。
Sub code_on_2016_with_LongLong()
Dim i As LongLong
i = InputBox("input integer number")
ReDim a(i) As Variant
'...
End Sub
Run Code Online (Sandbox Code Playgroud)
有人能告诉我为什么我不能在 Excel 2016 VBA 中 ReDim 具有 LongPtr 或 LongLong 类型索引的数组吗?
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
Run Code Online (Sandbox Code Playgroud)