为什么我不能在Excel 2016 VBA中使用LongPtr或LongLong索引重新编写数组

问题描述 投票:3回答:1

我习惯在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

最近,我将我的PC升级到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)错误。

即使我用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

有人可以告诉我为什么我不能在Excel 2016 VBA中使用LongPtr或LongLong类型的索引重新编写数组吗?

excel-vba vba excel
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.