vba - Excel Adding Worksheets -
i need create sub create worksheets based off of list of names in worksheet named allcities. list of city names starts in cell a2. worksheets need named after cell value in list, , should not create duplicate sheets. have far:
sub addsheets() dim mycell range dim cities range sheets("allcities") set cities = sheets("allcities").range("a2") set cities = range(cities, cities.end(xldown)) end each mycell in cities if not mycell.value = vbnullstring sheets.add after:=sheets(sheets.count) sheets(sheets.count).name = mycell.value end if next mycell end sub
it looks question around ensuring duplicates not created. think of 2 ways , have chosen believe efficient situation.
- remember names (chosen) - remember names of sheets in string can checked, not best solution if had large (25+ in length) city names across thousands of tabs, @ point suspect have different issues consider.
- create error handling proc check - call out second procedure check if sheet existed, make slower processing time safer if used on large scale.
below code check duplicates included.
sub addsheets() dim mycell range dim cities range dim strsheets string dim wksht excel.worksheet thisworkbook.worksheets("allcities") set cities = range(.range("a2"), .range("a2").end(xldown)) end strsheets = "|" each wksht in thisworkbook.worksheets strsheets = strsheets & wksht.name & "|" next each mycell in cities if not mycell.value = vbnullstring if instr(1, strsheets, "|" & mycell.value & "|") = 0 sheets.add after:=sheets(sheets.count) sheets(sheets.count).name = mycell.value strsheets = strsheets & mycell.value & "|" end if end if next mycell end sub
Comments
Post a Comment