excel - Subscript Out of Range for Looping Through Folder Code -
i trying loop through folder following code. however, keep getting subscript out of range error. explain fix issue?
sub loopthroughfolder() const filespec string = "*.xls" dim y integer dim myfolder string dim myfile string dim idot integer dim fileroot string dim fileext string dim arraydata() variant y = 2009 2030 redim preserve arraydata(y, 12) myfolder = activeworkbook.path & "\" & y & "\" = 1 myfile = dir(myfolder & filespec) while len(myfile) > 0 idot = instrrev(myfile, ".") if idot = 0 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) = + 1 loop next y end sub
according msdn link, can redim preserve
on last dimension of array, in case, change 12
in redim preserve arraydata(y, 12)
. changing order of array parameters fix problem.
Comments
Post a Comment