Create Worksheets From Number List

Small Paul

Board Regular
Joined
Jun 28, 2018
Messages
118
Hi

I have a primary worksheet with rows which are numbered according to set criteria. The current list is '1, 2, 2, 3, 4, 5, 5' etc down to 23.

I have the following code which was posted by 'mumps' in 2015 (thank you mumps):
Code:
    Dim bottomA As Long    bottomA = Range("A" & Rows.Count).End(xlUp).Row
    Dim c As Range
    Dim ws As Worksheet
    For Each c In Range("A2:A" & bottomA)
        Set ws = Nothing
        On Error Resume Next
        Set ws = Worksheets(c.Value)
        On Error GoTo 0
        If ws Is Nothing Then
                Worksheets.Add(After:=Sheets(Sheets.Count)).Name = c.Value
        End If
    Next c

It works great, with 1 exception - the first new worksheet created is named 3. So, sheets 3-23 have been created but not 1 and 2.

It is probably obvious to those of you who know what you are doing so any assistance would be greatly appreciated.

As an addition, is it possible to format ALL the worksheets in the same style in one go? For example, I need cells B12:L12 to be dark grey fill and cell B10 to be green.

Many thanks
Small Paul.
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
It's nice to see that some of my macros are still useful. :) Try this version:
Code:
Sub createSheets()
    Dim bottomA As Long
    bottomA = Range("A" & Rows.Count).End(xlUp).Row
    Dim c As Range
    Dim ws As Worksheet
    For Each c In Range("A2:A" & bottomA)
        Set ws = Nothing
        On Error Resume Next
        Set ws = Worksheets(CStr(c))
        On Error GoTo 0
        If ws Is Nothing Then
                Worksheets.Add(After:=Sheets(Sheets.Count)).Name = c.Value
        End If
    Next c
End Sub
 
Upvote 0
Hi mumps

That works perfectly - I now have new worksheets 1-23.
Thank you very much for the original macro and this update.

Are you able to suggest anything for my 2nd question? As an addition, is it possible to format ALL the worksheets in the same style in one go? For example, I need cells B12:L12 to be dark grey fill and cell B10 to be green.

Cheers
Small Paul.
 
Upvote 0
Try:
Code:
Sub createSheets()
    Dim bottomA As Long
    bottomA = Range("A" & Rows.Count).End(xlUp).Row
    Dim c As Range
    Dim ws As Worksheet
    For Each c In Range("A2:A" & bottomA)
        Set ws = Nothing
        On Error Resume Next
        Set ws = Worksheets(CStr(c))
        On Error GoTo 0
        If ws Is Nothing Then
            Worksheets.Add(After:=Sheets(Sheets.Count)).Name = c.Value
            Range("B10").Interior.ColorIndex = 4
            Range("B12:L12").Interior.ColorIndex = 48
        End If
    Next c
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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