Excel macro: Code for combining data from specific worksheets and refresh? -
i have workbook has 20 sheets. 11 of sheets data-based have external connections update , add lines of data entries added , workbook refreshed. other worksheets sheets tables , charts. trying use vba combine data 11 data-based sheets 1 sheet , have combined sheet refresh , update more entries added individual 11 sheets.
i have code combines specific 11 sheets, however, need on refresh part. tried adding button re-run code, deletes , re-adds combined sheet, messes references in formulas. i'm hoping able re-write of code combined sheet doesn't deleted , new 1 doesn't have added, , data can updated combined sheet without screwing references.
thanks.
here code have far...
sub copyrangefrommultiworksheets() dim sh worksheet dim destsh worksheet dim last long dim copyrng range application .screenupdating = false .enableevents = false end 'delete sheet "combinedreport" if exist application.displayalerts = false on error resume next activeworkbook.worksheets("combinedreport").cells.clearcontents on error goto 0 application.displayalerts = true 'add worksheet name "combinedreport" set destsh = activeworkbook.worksheets.add destsh.name = "combinedreport" 'loop through worksheets , copy data destsh each sh in activeworkbook.sheets(array("ucdp", "ucd", "uldd", "pe-wl", "emorttri", "emort", "earlycheck", "du", "do", "cdds", "cfds")) last = destsh.cells.specialcells(xlcelltypelastcell).row 'fill in range want copy set copyrng = sh.usedrange set copyrng = copyrng.offset(1, 0).resize(copyrng.rows.count - 1, copyrng.columns.count) 'test if there enough rows in destsh copy data if last + copyrng.rows.count > destsh.rows.count msgbox "there not enough rows in destsh" goto exitthesub end if 'this example copies values/formats, if want copy 'values or want copy @ example below macro copyrng.copy destsh.cells(last + 1, "a") .pastespecial xlpastevalues .pastespecial xlpasteformats application.cutcopymode = false end next exitthesub: application.goto destsh.cells(1) 'autofit column width in destsh sheet destsh.columns.autofit application .screenupdating = true .enableevents = true end end sub
Comments
Post a Comment