string - Loop through the workbook but exclude some worksheets? -
this question has been asked around net several times without comprehensive program in within macro. have data on sheets need analyse data more on separate 'analysis sheets'. believe problem may thisworkbook.sheets("sh")
function gives off subscript out of range
error.
below code, respond changes make make code work!
sub excludesheet() dim sh worksheet const excludesheets string = "1-analysis,2-analysis" 'assigns worksheet object sh variable each sh in thisworkbook.worksheets if iserror(application.match(sh.name, split(excludesheets, ","))) 'this analysis worksheets range("$a$1").value = "analysis" else: 'data sheets columns("a:m").autofit lr = cells(rows.count, "a").end(xlup).row = lr 2 step -1 if cells(i, "e").text = "n/a" rows(i).entirerow.delete next lastr = cells(rows.count, "a").end(xlup).row dim strformulas(1 3) variant thisworkbook.sheets("sh") strformulas(1) = "=(e2-$e$2)" strformulas(2) = "=(g2-$g$2)" strformulas(3) = "=h2+i2" range("h2:j2").formula = strformulas range("h2:j" & lastr).filldown end end if next end sub
to further clarify comment, work on objects directly.
check out various ways on how you'll that.
now try refactored code of yours:
sub excludesheet() dim sh worksheet 'const excludesheets string = "1-analysis,2-analysis" dim excludesheets: excludesheets = array("1-analysis", "2-analysis") each sh in thisworkbook.worksheets sh 'you have sheet object, work if not iserror(application.match(.name, excludesheets, 0)) 'this analysis worksheets .range("$a$1").value = "analysis" else 'data sheets .columns("a:m").autofit lr = .cells(.rows.count, "a").end(xlup).row = lr 2 step -1 if .cells(i, "e").text = "n/a" .rows(i).entirerow.delete next lastr = .cells(.rows.count, "a").end(xlup).row dim strformulas(1 3) variant strformulas(1) = "=(e2-$e$2)" strformulas(2) = "=(g2-$g$2)" strformulas(3) = "=h2+i2" .range("h2:j2").formula = strformulas .range("h2:j" & lastr).filldown end if end next end sub
Comments
Post a Comment