Add Sheets per Column Cell Contents (VBA)

nickwilshaw

New Member
Joined
Apr 18, 2011
Messages
13
Hi Guys,

I need sonme help with VBA code so as to add sheets to a workbook per the contents of a list ( a column in a sheet).

For Example, say sheet "Customers" contains 25 customer names in Column A - eg A,B,C,D etc then I would want 25 sheets to be added and for those sheets to be named A,B,C,D etc.

Thus far, I have been trying to develop:

Sub NameWorksheet()
Range("A2").Select ' cos A1 is the header "Customer"
Do While IsEmpty(Sheets("Customer").Range("A2").Offset(0, 1)) = False
Sheets.Add.Name = ActiveCell.Text
ActiveCell.Offset(1, 0).Select

Loop

End Sub

However, a principal stumbling block is the

Sheets.Add.Name = ActiveCell.Text as VBA want a range, say, ="A2" rather than = ActiveCell.

Equally, I may be approaching this the wrong way and any pointers would again be most appreciated.

Best regards and many thanks
Nick
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Code:
[COLOR="Blue"]Sub[/COLOR] NameWorksheet()
    [COLOR="Blue"]Dim[/COLOR] i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR]
    Application.ScreenUpdating = False
    [COLOR="Blue"]With[/COLOR] Sheets("Customer")
        [COLOR="Blue"]For[/COLOR] i = 1 [COLOR="Blue"]To[/COLOR] .Range("A2").End(xlToRight).Column
            Sheets.Add(After:=Sheets(Sheets.Count)).Name = .Cells(2, i)
        [COLOR="Blue"]Next[/COLOR]
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Last edited:
Upvote 0
Code:
Sub NameWorksheet()
Dim Last_Row As Long
Dim i As Long
Application.ScreenUpdating = False
With Sheets("Customers")
    Last_Row = .Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 2 To Last_Row
        Sheets.Add().Name = .Cells(i, 1).Value
    Next i
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi njimack,

Many thanks for that - words a treat!

Just out of curioisity - what is the rationale behind the syntax of the Last_Row statement.

I appreciate that it is trying to count how many rows (have something in them?) but what does the xlup do for example and why does it need to be defined in this way?

Again, many thanks for your help
 
Last edited:
Upvote 0
Hi Sektor,

Thanks for the post but this actually adds sheets named according to the content of Row A whereas I wanted it based on Column A.

Will be a useful post for others as I am sure that there will be those who need to achieve this result as well!
 
Upvote 0
Code:
[COLOR="Blue"]Sub[/COLOR] NameWorksheet()
    [COLOR="Blue"]Dim[/COLOR] i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR]
    [COLOR="Blue"]With[/COLOR] Sheets("Customer")
        [COLOR="Blue"]For[/COLOR] i = 1 [COLOR="Blue"]To[/COLOR] .Range("A2").End(xlDown).Row
            Sheets.Add(After:=Sheets(Sheets.Count)).Name = .Cells(i, 1)
        [COLOR="Blue"]Next[/COLOR]
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Just out of curioisity - what is the rationale behind the syntax of the Last_Row statement.

I appreciate that it is trying to count how many rows (have something in them?) but what does the xlup do for example and why does it need to be defined in this way?

Select the very last row in your sheet (row 65536 in 2003) in a column with data. Then hold the ctrl key down and press the up arrow. You should land on the first piece of data found in that column, searching from the bottom, up.

In effect, that's what the piece of code is doing.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,845
Members
452,948
Latest member
UsmanAli786

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