我正在尝试使用以下代码遍历文件夹。但是,我不断收到下标超出范围错误。谁能解释我能做些什么来解决这个问题?
Sub LoopThroughFolder()
Const FileSpec As String = "*.xls"
Dim y As Integer
Dim MyFolder As String
Dim MyFile As String
Dim iDot As Integer
Dim FileRoot As String
Dim FileExt As String
Dim ArrayData() As Variant
For y = 2009 To 2030
ReDim Preserve ArrayData(y, 12)
MyFolder = ActiveWorkbook.Path & "\" & y & "\"
i = 1
MyFile = Dir(MyFolder & FileSpec)
Do While Len(MyFile) > 0
iDot = InStrRev(MyFile, ".")
If iDot = 0 Then
FileRoot = MyFile
FileExt = ""
Else
FileRoot = Left(MyFile, iDot - 1)
FileExt = Mid(MyFile, iDot - 1)
End If
MyFile = Dir
ArrayData(y, i) = FileRoot
MsgBox ArrayData(y, i)
i = i + 1
Loop
Next y
End Sub