为什么在数组分配期间出现溢出错误?

时间:2019-10-24 19:09:39

标签: excel vba

我有一个设置一些全局数组的程序。该程序在几个小时之前运行良好。今天已经运行了几次。除了数组的大小(从99500-99900,9),我什么都没改变,这是问题吗?如果它是数组的大小,是否有办法用不同的数据结构(集合,字典,用户定义的)来存储大量数据?

    Const PartMaterialsFilePath = "C:\Users\jayferreira\Desktop\"
    Const PartMaterialsFileName = "Part Materials.xlsm"

    Function getPPNWorksheet() As Worksheet
        If isWkbkOpen(PartMaterialsFileName) Then
            Set getPPNWorksheet = Workbooks(PartMaterialsFileName).Worksheets(2)
        Else
            Set getPPNWorksheet = Workbooks.Open(PartMaterialsFilePath + PartMaterialsFileName).Worksheets(2)
        End If
    End Function

    Global partDescriptions() As String
    Global partRevisions() As String * 5
    Global partRevisionDates() As String * 10
    Global partWeights() As Single
    Global partNumbers() As String * 6
    Global PartMaterials As Dictionary
    Global initialized As Boolean
    Function setGlobals()
        Application.ScreenUpdating = False
        Load loadingBar
        loadingBar.Show
        Dim rng As Range
        Dim dataArr() As Variant
        Dim mGPPN As Worksheet
        Dim multipleOf As Long
        Dim counter As Integer
        Dim lastRow As Long
        Dim i As Long
        loadingBar.Text.Caption = "Loading ppn.xlsx..."
        Set mGPPN = MCGBookFunctions.getPPNWorksheet
        lastRow = MCGBookFunctions.getLastRow(mGPPN, , 1, 1)
        loadingBar.Text.Caption = "Loading Data..."
        mGPPN.Activate
        dataArr = mGPPN.Range("A2", "I" & lastRow) ''this is where the overflow error occurs
        MCGBookFunctions.closePartMaterialsworkbook True ''closes mGPPN
        Set mGPPN = Nothing
        ReDim partDescriptions(lastRow)
        ReDim partRevisions(lastRow)
        ReDim partRevisionDates(lastRow)
        ReDim partWeights(lastRow)
        ReDim partNumbers(lastRow)
        multipleOf = lastRow / 100
        For i = 1 To UBound(dataArr, 1)
            partNumbers(i) = dataArr(i, 1)
            partDescriptions(i) = dataArr(i, 2)
            partRevisions(i) = dataArr(i, 3)
            partRevisionDates(i) = dataArr(i, 4)
            partWeights(i) = dataArr(i, 5) * 1000 
            If i Mod multipleOf = 0 Then
                counter = counter + 1
                loadingBar.Caption = "Storing Data..."
                loadingBar.Text.Caption = counter & "% Completed"
                loadingBar.Bar.Width = counter * 2
                DoEvents
            End If
        Next
        loadingBar.Caption = "Getting part materials from database..."
        Set PartMaterials = DataBase.GetMaterials()
        initialized = True
        Application.ScreenUpdating = True
        Unload loadingBar
    End Function

注意:引发错误的那一行是在重新设置数组之前的两行。如代码中所注释的,“ dataArr = mGPPN.Range(“ A2”,“ I”和lastRow)“。

注意:我尝试使用范围大小,并且可以将Range(“ A2”,“ I7804”)分配给数组,但不能再增加一行。今天,直到数小时前,我已经多次为阵列分配了更大的范围。

1 个答案:

答案 0 :(得分:0)

.Value2中使用mGPPN.Range("A2", "I" & lastRow).Value2解决了我的问题。我认为这与单元格的格式化方式有关,也许在尝试将日期(Date)存储为字符串时会产生溢出?