Creating multiple sheets automatically

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Some are repeated because there is more than 1 location in the city but I still need a seperate sheet for each time. So, I will need (2) Carson, CA sheets and, etc...
 
Upvote 0
So are you saying you want a script to run and.
Look in each cell in column J and create a new sheet named the value in column J

So if J1 has Alpha you want a new sheet added to your workbook named Alpha
And if J2 has Bravo you want a new sheet added to your workbook named Bravo

And what is the name of the sheet with all these values in column J

And you cannot have two sheets with the same name.
 
Upvote 0
I can work around the double name by changing it up a bit, but yes, if J2 says Alpha, sheeta 1 to say Alpha, etc... just like you described
 
Upvote 0
How about
Code:
Sub AddSheets()
   Dim cl As Range
   Dim Ky As Variant
   Dim i As Long
   
   With CreateObject("Scripting.dictionary")
      For Each cl In Range("J2", Range("J" & Rows.Count).End(xlUp))
         .Item(cl.Value) = .Item(cl.Value) + 1
      Next cl
      For Each Ky In .keys
         If .Item(Ky) = 1 Then
            Sheets.Add.name = Ky
         Else
            For i = 1 To .Item(Ky)
               Sheets.Add.name = Ky & " (" & i & ")"
            Next i
         End If
      Next Ky
   End With
End Sub
 
Upvote 0
If the worksheet exists, then you can copy the first sheet with that name, then it will automatically index it with sheetname(2),sheetname(3), if the sheet does not exist, then it will add a new sheet and name it.
This example will assume the data is in column J, Sheet1

Code:
Sub AddSheets()
    Dim rng As Range, c As Range, LstRw As Long, sh As Worksheet
    Dim worksh As Integer
    Dim worksheetexists As Boolean

    Set sh = Sheets("Sheet1")

    With sh
        LstRw = .Cells(.Rows.Count, "J").End(xlUp).Row
        Set rng = .Range("J2:J" & LstRw)
    End With

    Application.ScreenUpdating = False

    For Each c In rng.Cells
        worksh = Application.Sheets.Count
        worksheetexists = False
        For x = 1 To worksh
            If Worksheets(x).Name = c Then
                worksheetexists = True
                Sheets(c.Value).Copy After:=Worksheets(Sheets.Count)
                'MsgBox c & ", already Exists"
                Exit For
            End If
        Next x
        If worksheetexists = False Then
            'MsgBox "Nope"
            Sheets.Add After:=Worksheets(Sheets.Count)
            ActiveSheet.Name = c
        End If

    Next c

    sh.Select


End Sub

Just for convenience, here is a code to delete all the sheet except sheet1

Code:
Sub DeleteShts()
    Dim sh As Worksheet
    Application.DisplayAlerts = False
    For Each sh In Sheets
        If sh.Name <> "Sheet1" Then sh.Delete
    Next sh
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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