Create new sheets named after each cell in a range. Do nothing if sheet is already created. Create if not.

Marzagao

New Member
Joined
Apr 10, 2014
Messages
4
Hi all

I have the following code but need to tweak it so that on the 2nd time it runs, it no longer creates every single sheet, but only new sheets (not yet on the workbook). Can anyone help? The purpose is to create new sheets based on sheet 'Template'. Also I'm having some trouble on the macro to just run through Range "B2:B6" it seems like it goes all the way down in column B... Thanks in advance!!

Sub CreateSheetsFromAList2()


Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Worksheets("Template")


Dim MyCell As Range, MyRange As Range


Set MyRange = Sheets("Advertisers").Range("B2:B6")
Set MyRange = Range(MyRange, MyRange.End(xlDown))


For Each MyCell In MyRange
ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
ThisWorkbook.Worksheets("Template (2)").Name = MyCell.Value
Next MyCell


End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
How about a remodeling job.
Code:
Sub CreateSheetsFromAList3()
Dim ws1 As Worksheet, rng As Range, c As Range, test As String
Set ws1 = Worksheets(1) '("Template")
Set rng = ws1.Range("B2", ws1.Cells(Rows.Count, 2).End(xlUp))
    For Each c In rng
        On Error Resume Next
        If Sheets(c.Value) Is Nothing Then
            If Err.Number > 0 Then
                Err.Clear
                ws1.Copy After:=ThisWorkbook.Sheets(Sheets.Count)
                ActiveSheet.Name = c.Value
            End If
        End If
    Next
End Sub
 
Upvote 0
it goes all the way down, but you start B2 to B6, and then go down which will extend

if your going to do this in frequently, check out ASAP utilities, it does this stuff in its sleep
 
Upvote 0
Marzagao,

Similar to Whiz but in the belief that you wish to restrict to range B2:B6 which is within sheet 'Advertisers' ???

Code:
Sub CreateSheetsFromAList2()Dim ws, ws1 As Worksheet
Dim MyCell As Range, MyRange As Range
Set ws1 = ThisWorkbook.Worksheets("Template")
Set MyRange = Sheets("Advertisers").Range("B2:B6")
For Each MyCell In MyRange
    If Not MyCell = "" Then
        On Error Resume Next
        Set ws = Nothing
        Set ws = ThisWorkbook.Worksheets(MyCell.Value)
        On Error GoTo 0
        If ws Is Nothing Then
            ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
            ThisWorkbook.Worksheets("Template (2)").Name = MyCell.Value
        End If
    End If
Next MyCell
End Sub
 
Upvote 0
Thanks guys, I can only post back results in a couple of hours but the code seems pretty good and logic. Snakehips, yes B2:B6 in Advertisers. Thanks for that!
 
Upvote 0

Forum statistics

Threads
1,214,895
Messages
6,122,128
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top