excel - Macro exiting early with no error -
i using below code copy in workbooks particular folder 1 workbook. 9 out of 10 times code works fine , data copied macro appears exit without finishing msgbox never displays , not error message. macro appears have been exited allows me run other macros. can advise me might causing this? seems happen if start other things on computer while macro running.
sub getsheets() application.screenupdating = false dim response response = msgbox("this take time run. sure want proceed?", vbyesno) if response = vbno exit sub end if application.run ("getxlsxfiles") application.run ("getxlsfiles") datacopied = 1 sheets("instructions").select msgbox "completed successfully" end sub sub getxlsxfiles() dim sheet worksheet path = sheets("instructions").range("filename").value filename = dir(path & "*.xlsx") while filename <> "" workbooks.open filename:=path & filename, readonly:=true, password:="password" each sheet in activeworkbook.sheets sheet.copy after:=workbooks("rsmodel.xlsm").sheets("current kpis") next sheet workbooks(filename).close savechanges:=false filename = dir() loop end sub
the getxlsfiles sub exact same above except file extension.
i've re-written code , provide comments in there.
there's many fit in comment.
here's getxlsxfiles sub
:
it's brief if remove comments explains did.
sub getxlsxfiles() dim wb workbook, wbtemp workbook dim path string, filename string ', masterwb string dim sheet worksheet '~~> assuming path correct path = sheets("instructions").range("filename").value '~~> path should contain e.g. "c:\testfolder\" filename = dir(path & "*.xlsx") '~~> assuming consolidating sheets '~~> in workbook contain macro set wb = thisworkbook '~~> if not, use commented line below '~~> take note not include file extension 'set wb = workbooks("rsmodel") '~~> or can open 'masterwb = "c:\foldername\rsmodel.xlsm" 'set wb = workbooks.open(filename:=masterwb, readonly:=true) while filename <> "" set wbtemp = workbooks.open(filename:=path & filename, readonly:=true, _ password:="password") each sheet in wbtemp.sheets '~~> adds sheet after last sheet in target wb '~~> if want add after specific sheet, '~~> use commented line sheet.copy after:=wb.sheets(wb.sheets.count) 'sheet.copy after:=wb.sheets("current kpis") next wbtemp.close false filename = dir loop end sub
here getsheets sub
:
sub getsheets() application.screenupdating = false dim response integer response = msgbox("this take time run." & vbnewline & _ "are sure want proceed?", vbyesno) '~~> execute if in 1 line if response = vbno exit sub '~~> no need use application.run. call subs directly getxlsxfiles getxlsfiles '~~> not sure what's commented 'datacopied = 1 '~~> if want below sheet selected thisworkbook.sheets("instructions").select msgbox "completed successfully", vbinformation end sub
i think above should close want.
hope helps.
Comments
Post a Comment