excel - Deactivate entire sheet selection after paste -
i asked question , received great answer on site, running different problem. code below works running through each workbook in folder, copying sheet's contents, , pasting contents master workbook how like:
sub conslidateworkbooks() 'code pull sheets multiple excel files in 1 file directory 'into master "consolidation" sheet. dim folderpath string dim filename string dim sheet worksheet dim wbname string activesheet range("a1").activate end application.screenupdating = false folderpath = activeworkbook.path & "\" filename = dir(folderpath & "*.xls*") wbname = activeworkbook.name while filename <> "" if filename <> wbname workbooks.open filename:=folderpath & filename, readonly:=true each sheet in activeworkbook.sheets copyorrefreshsheet thisworkbook, sheet next sheet workbooks(filename).saved = true workbooks(filename).close activesheet.range("a1").activate end if filename = dir() loop application.screenupdating = true end sub sub copyorrefreshsheet(destwb workbook, sourcews worksheet) dim ws worksheet on error resume next set ws = destwb.worksheets(sourcews.name) on error goto 0 if ws nothing sourcews.copy after:=destwb.worksheets(destwb.worksheets.count) else ws.unprotect password:="abc123" ws.cells.clearcontents sourcews.usedrange.copy ws.range(sourcews.usedrange.address).pastespecial (xlpasteall) application.cutcopymode = false end if end sub
the problem having now: after paste completed, each sheet in master workbook has of cells selected, though ctrl+a
'd entire sheet. rid of this. small task tried accomplish in line activesheet.range("a1").activate
within do while ..
loop, has not worked me.
edit:
i found solution works in case. not sure why necessary, because comments , answers in thread seem should work, did not. call sub
before turn screenupdating
true
in main sub:
sub selecta1() worksheets(1).activate dim sheet worksheet each sheet in activeworkbook.sheets sheet.activate activewindow.scrollrow = 1 activewindow.scrollcolumn = 1 sheet.range("a1").select next sheet worksheets(1).activate end sub
i realize more complicated should be, works purposes.
in copy sub, add in code in loop select cell should deactivate total used range selection , select coded range.
sub copyorrefreshsheet(destwb workbook, sourcews worksheet) dim ws worksheet on error resume next set ws = destwb.worksheets(sourcews.name) on error goto 0 if ws nothing sourcews.copy after:=destwb.worksheets(destwb.worksheets.count) else ws.unprotect password:="abc123" ws.cells.clearcontents sourcews.usedrange.copy ws.range(sourcews.usedrange.address).pastespecial (xlpasteall) ws.range("a1").select application.cutcopymode = false end if end sub
i added ws.range("a1").select
should described above.
Comments
Post a Comment