vba - Excel Macro: Can someone replace the delete and add sheet lines with one that clears the content of the sheet? -


i have macro combines specific sheets , works fine except 1 thing. want combined sheet able update , refresh , entries added individual sheets, , have formulas on other sheets reference combined sheet. in code combining, combined sheet gets deleted if present , added again, messes formula references. therefore, edit code , remove part deletes , re-adds combined sheet , instead clear contents of sheet combining data.

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").delete     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 

this me out lot.. thank effort.

i think should it. assume formulae on other sheets , refer destination sheet? code assume have "combinedreport" sheet start with.

sub x()  application     .screenupdating = false     .enableevents = false end  set destsh = activeworkbook.sheets("combinedreport") destsh.usedrange.clearcontents  '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.range("a" & rows.count).end(xlup).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

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -