VBA阵列大小重置一次达到32767(Excel 2010)

时间:2017-06-14 03:44:02

标签: arrays excel vba excel-vba

我使用一维动态数组来保存它们的总数将为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

1 个答案:

答案 0 :(得分:2)

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

这将触发超过~32k的越界误差,然后触发您的阵列重置。