excel - Loop through workbooks in a folder to copy and insert cells into a master workbook -
i have loooop through workbooks in folder start phrase "batch" copy , insert copied cells 1 sheet in master workbook. i've tried use example found online isn't working. doesn't anything.
sub runcodeonallxlsfiles() dim lcount long dim wbresults workbook dim wbcodebook workbook application.screenupdating = false application.displayalerts = false application.enableevents = false on error resume next set wbcodebook = thisworkbook application.filesearch .newsearch .lookin = "c:\path" .filetype = msofiletypeexcelworkbooks .filename = "batch*.xls" if .execute > 0 lcount = 1 .foundfiles.count set wbresults = workbooks.open(filename:=.foundfiles(lcount), updatelinks:=0) workbooks(filename).worksheets("data").range("b23:z38").copy thisworkbook.worksheets("sheet1").range("b2").rows("1:16").insert shift:=xldown wbresults.close savechanges:=false next lcount end if end on error goto 0 application.screenupdating = true application.displayalerts = true application.enableevents = true end sub
i'd able have 1 file can dropped folder perform task.
as andy g pointed in comment, forgot pasting. coude shoud be
workbooks(filename).worksheets("data").range("b23:z38").copy thisworkbook.worksheets("sheet1").range("b2").rows("1:16").insert shift:=xldown thisworkbook.worksheets("sheet1").range("b2").paste
edit: since application.filesearch
is gone of excel 2007, try alternative approach using vba's dir()
function:
sub runcodeonallxlsfiles() dim wbcodebook workbook dim mypath string dim mymask string dim fnresults string dim wbresults workbook application.screenupdating = false application.displayalerts = false application.enableevents = false set wbcodebook = thisworkbook mypath = "c:\path" mymask = "batch*.xls" fnresults = dir(mypath & "\" & mymask) 'get 1st match while fnresults <> "" set wbresults = workbooks.open(mypath & "\" & fnresults, 0) workbooks(fnresults).worksheets("data").range("b23:z38").copy thisworkbook.worksheets("sheet1").range("b2").rows("1:16").insert shift:=xldown thisworkbook.worksheets("sheet1").range("b2").paste fnresults = dir 'get next match loop application.screenupdating = true application.displayalerts = true application.enableevents = true end sub
Comments
Post a Comment