loops - Copy-Paste Efficiency and an Array of Worksheets in VBA -


i have following code pasting range of cells in array of worksheets - not of them:

dim ws worksheet dim decisionworksheets dim mypassword string dim copyrange dim wf worksheetfunction  set wf = application.worksheetfunction set decisionworksheets = sheets(array("admin", "lm", "ca", "fm", "gd")) each ws in decisionworksheets     ws.unprotect mypassword     ws.cells.clearcontents next ws  rownum = wf.max(range("b:b")) copyrange = worksheets("data").range(cells(4, 2), cells(rownum + 3, 6)).copy  each ws in decisionworksheets     ws.range("b7").pastespecial xlpastevalues next ws  each ws in decisionworksheets     ws.protect mypassword next ws 

each of worksheets in array above password protected , if put "ws.unprotect mypassword" inside second loop, cut-copy mode disabled. because of this, created separate "for each" loop, first go through array of worksheets unprotect them (1st loop) , protect them again (3rd loop).

my question efficiency of code. namely, wondering whether there way unite these 3 loops single one. other suggestions appreciated!

i believe paste code did not work because clearing worksheet cells in loop canceled copy mode. if copy range go sheet , delete cells, no longer have cells copied. may have copy , paste after clear cells. @ example.

sub button1_click()     dim ws worksheet     dim decisionworksheets     dim mypassword string     dim sh worksheet, lstrw long, copyrng range      mypassword = "123"     set decisionworksheets = sheets(array("admin", "lm", "ca", "fm", "gd"))      set sh = sheets("data")      sh         lstrw = .cells(.rows.count, "a").end(xlup).row         set copyrng = .range(.cells(4, 2), .cells(lstrw + 3, 6))     end      application.screenupdating = 0     each ws in decisionworksheets         ws.unprotect mypassword         ws.cells.clearcontents         copyrng.copy         ws.range("b7").pastespecial xlpastevalues         ws.protect mypassword     next ws  end sub 

Comments