次のコードでフォルダーをループしようとしています。ただし、添え字が範囲外のエラーを出し続けます。この問題を解決するために私ができることを誰か説明してもらえますか?
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