【问题标题】:VBA array size reset once hitting 32767 (Excel 2010)一旦达到 32767(Excel 2010),VBA 数组大小就会重置
【发布时间】:2017-06-14 03:44:02
【问题描述】:

我使用一维动态数组来保存它们的总数将超过 60K 的值。这似乎是一项简单的任务,但我注意到一旦数组的大小达到 32767,它又从 0 开始。任何建议将不胜感激。

我的代码:

Sub GetHours()

Dim R As Long, i As Long, N As Long, var, vRaw, v

R = LastUsedRow(Sheet1)
With Sheet1
      vRaw = .Range(.Cells(2, 1), .Cells(R, 22)).Value
End With

For i = 1 To R - 1
    var = vRaw(i, 12)
    If IsNumeric(var) Then
        If IsArrayEmpty(v) Then
            ReDim v(0)
            v(0) = i
        Else
            N = UBound(v) + 1
            ReDim Preserve v(N)
            v(N) = i
        End If

    End If
Next

End Sub

Function LastUsedRow(ByVal ws As Worksheet) As Long

Dim lastrow As Long

On Error GoTo errHandler
lastrow = ws.Cells.Find(What:="*", After:=[A1], _
                         SearchOrder:=xlByRows, _
                         SearchDirection:=xlPrevious).Row

LastUsedRow = lastrow
Exit Function

errHandler:
    LastUsedRow = 0

End Function

Function IsArrayEmpty(anArray As Variant)
Dim i As Integer
On Error Resume Next
i = UBound(anArray, 1)

If Err.Number = 0 Then
    IsArrayEmpty = False
Else
    IsArrayEmpty = True
End If
End Function

【问题讨论】:

  • 让我猜猜:您使用的是 2013 年之前版本的 Excel,这是此 Excel 电子表格允许的最大范围,这就是它重置的原因。如果不是这种情况,请将您正在使用的 Excel 版本添加到您的问题中。
  • 您可能对此感兴趣:stackoverflow.com/a/18558328/3380970
  • LastUsedRow 的代码在哪里?
  • IsArrayEmpty 也可能很重要...
  • 暂停ReDim v(0) 看看它何时运行。请注意,您在最后一个函数中有 Dim i As Integer... 这将触发超过 ~32k 的越界错误,然后触发您的数组被重置

标签: arrays vba excel


【解决方案1】:

请注意,您在最后一个 IsArrayEmpty 函数中有 Dim i As Integer...

这将触发超过 ~32k 的越界错误,然后触发您的数组被重置。

【讨论】:

  • 确实如此。再次,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
  • 2019-02-26
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-24
相关资源
最近更新 更多