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.

  1. 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.
  2. 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

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -