excel - VBA loop through directory -
**hi all,
i incorporate below script ability search through files , export data recent file in folder. adding new file every week folder not want old data range copied across.
can please help?**
sub loopthroughdirectory() dim myfile string dim erow fileroot = "c:\users\ramandeepm\desktop\consolidate\" myfilename = dir("c:\users\ramandeepm\desktop\consolidate\") while len(myfilename) > 7 if myfilename = "zmaster.xlsm" exit sub end if myfile = fileroot & myfilename workbooks.open (myfile) range("range").copy activeworkbook.close erow = sheet1.cells(rows.count, 1).end(xlup).offset(1, 0).row activesheet.paste destination:=worksheets("sheet1").range(cells(erow, 1), cells(erow, 4)) myfilename = dir() loop end sub
if use filesystemobject
can done using .datelastmodified
property. below code should started:
untested
dim fso filesystemobject dim objfile file dim myfolder dim strfilename string dim dtfile date 'set folder location const mydir string = "c:\users\ramandeepm\desktop\consolidate" 'set filesys objects set fso = new filesystemobject set myfolder = fso.getfolder(mydir) 'loop through each file , date last modified. if largest date store filename dtfile = dateserial(1900, 1, 1) each objfile in myfolder.files if len(objfile.name) > 7 if objfile.datelastmodified > dtfile dtfile = objfile.datelastmodified strfilename = objfile.name end if end if next objfile workbooks.open strfilename
note: code looking recent modified date. work if newest file created after modifications in other files in folder. also, may need enable microsoft scripting runtime
library reference.
Comments
Post a Comment