excel - Copying worksheets using dynamic array -
i want copy worksheets in new file. every file should saved per country - thing have different worksheets (clients) per country , list might change in future. created list able edit code in future, of course listed in excel worsheet:
sales org tabs be01 albro dk01 stockmann", "mister", "ginsborg it01 la rinascente", "arcobaleno
in column "tabs" listed sheets want copied per file , sales organization represents file name.
my code works in case of be01, when comes dk01 receive "subscript out of range" error...
can 1 tell me how fix this?
sub savefile() ' dim savefolder string dim filetype string dim filename string dim lastrow integer dim name string dim eufile string dim todaydate string dim list string lastrow = sheets("macro control").range("a1048576").end(xlup).row savefolder = sheets("macro control").range("d2") filetype = sheets("macro control").range("e2") filename = sheets("macro control").range("f2") todaydate = format(date, "dd.mm.yyyy") dim array_db() string redim array_db(lastrow - 2, 1) row_number = 2 lastrow array_db(row_number - 2, 0) = sheets("macro control").range("a" & row_number) array_db(row_number - 2, 1) = sheets("macro control").range("b" & row_number) next = 0 ubound(array_db) list = array_db(i, 1) sheets(array(list)).copy name = array_db(i, 0) eufile = savefolder & "\" & filename & " " & todaydate & " " & name & filetype activeworkbook.saveas filename:=eufile activeworkbook.close next end sub
you can use following pattern select multiple sheets dynamically:
dim sheetnames, long sheetnames = split("sheet1|sheet2|sheet3", "|") worksheets(sheetnames(0)).select = lbound(sheetnames) + 1 ubound(sheetnames) worksheets(sheetnames(i)).select false next
in other words, change column b sheet names separated suitable character, example pipe character ("|") , use above. worksheet.select method has option paramater named "replace", , setting false means sheet selected in addition selected sheet.
edit:
by way, don't have read cell values array 1 one. can in 1 step using variant array:
dim array_db() variant array_db = sheets("macro control").range("a2").resize(lastrow-1,2).value
it advisable specify dimension's ubound get: ubound(array_db,1). code works because finds first dimension's ubound default, not ubound want.
Comments
Post a Comment