-2
我真的很費力地找到在我的代碼下面生成'下標超出範圍'的錯誤。我所說的代碼是從當前日期90天內的文本文件中讀取具有時間戳的數據。爲了收集這些數據,我逐行讀取每個文件,將這些數據存儲到拆分數組中,然後使用拆分數組的特定行填充兩個集合。這段代碼可以很好地處理我用來從特定作業集中檢索數據的另一個應用程序。前面提到的應用程序中的代碼看起來工作正常,直到我到達一個與以前成功讀取的文件沒有任何差別(格式,內容或其他)的特定文件。在我看來,我已達到每個系列的尺寸限制,但我不確定。以下是我的代碼:VBA下標超出範圍集合包含如此多的元素後出現的錯誤
Private Sub Analyze_90Day_Data_Click()
Application.ScreenUpdating = False
Data_Selection_21075A.Hide
'Declare Variables
Dim FFFile As String
Dim SplitData() As String
Dim DateIter As Long
Dim CurrentDate As Date
Dim FileDate As Date
Dim colSN As String
Dim colSet As String
Dim KPCDate As Date
Dim CMMArray() As Variant
'Remove old data from 21075A KPC
LastRow = Sheets("21075A KPC").Range("G65536").End(xlUp).Row
For SheetRow = 12 To LastRow
Sheets("21075A KPC").Range("G" & SheetRow & ":H" & SheetRow).ClearContents
Next SheetRow
'Populate 21075A KPC with new data
CurrentDate = Date
KPCDate = DateAdd("d", -90, CurrentDate)
CMMArray = Array("Inspcmm1", "Inspcmm2")
For CMM = LBound(CMMArray) To UBound(CMMArray)
With New Scripting.FileSystemObject
CMMFolder = "\\" & CMMArray(CMM) & "\cmm\21075A\OP290"
On Error GoTo ResumeIter
Set CMMFold = .GetFolder(CMMFolder)
For Each SetFolder In CMMFold.SubFolders
FFFile = SetFolder & "\21075A-030-FINALFLOWTOT " & SetFolder.Name & ".txt"
MsgBox SetFolder.Name
FileDate = FileDateTime(FFFile)
If FileDate >= KPCDate Then
LineIter = 0
With .OpenTextFile(FFFile, ForReading)
Do Until .AtEndOfStream
LineIter = LineIter + 1
LineData = .ReadLine
SplitData = Split(LineData)
'Extracting Serial Number
strSN.Add SplitData(0)
'Extracting Final Flow Value
strFF.Add SplitData(2)
Loop
.Close
End With
End If
Next SetFolder
End With
ResumeIter:
Next CMM
If strSN.Count = 0 Then
MsgBox "Neither Brown & Sharpe is online."
Exit Sub
Else
SheetRow = 12
For SNIter = 1 To strSN.Count Step 1
With Sheets("21075A KPC")
.Range("G" & SheetRow).Value = strSN.Item(SNIter)
.Range("H" & SheetRow).Value = strFF.Item(SNIter)
End With
SheetRow = SheetRow + 1
Next SNIter
LastRow = Sheets("21075A KPC").Range("G65536").End(xlUp).Row
With Sheets("21075A KPC")
'Calculate & Populate Means
.Range("H5") = WorksheetFunction.Average(.Range("H12:H" & LastRow))
'Calculate & Populate Standard Deviations
.Range("H6") = WorksheetFunction.StDev(.Range("H12:H" & LastRow))
'Populate 90-Day Reporting Period
.Range("F3") = KPCDate
.Range("F4") = CurrentDate
End With
End If
End Sub
未在上述過程中聲明的所有變量都已公開聲明。感謝您查看這個。請讓我知道你是否需要我的其他東西。
請註釋或以其他方式披露錯誤正在產生的代碼行。如果沒有很多被忽略的東西,沒有人可以真正運行你的代碼,但是如果你告訴我們它發生了什麼,我們可能有機會確定問題所在。 – Jeeped
這可能是因爲他們的數組SplitData在位置2中沒有任何對於正在破解的行和文件有任何意義。 – OpiesDad
@Jeeped,發生錯誤的代碼行是'strSN.Add SplitData(0)'。如果我註釋掉這一行,該錯誤將跳轉到下一行'strFF.Add SplitData(2)'。 – jlynn303