Name worksheets from list

drluke

Active Member
Joined
Apr 17, 2014
Messages
312
Office Version
  1. 365
Platform
  1. Windows
I have the code below in an Add-In that creates and names 3 worksheets at the end of existing sheets. However, because of a change, I can no longer have the list with the worksheet names in the "Business Structure" worksheet.

I have searched for a solution, but was not able to find anything. Ideally I want the names of the 3 sheets to be "housed"directly in the macro itself (ie no need to refer to a list in a worksheet). The 3 files are created (in order) SPSJnl, AssJnl & VolJnl

Here is my code:
Code:
For i = 1 To 3
            Worksheets.Add after:=Worksheets(Worksheets.Count)
                Sheets(Worksheets.Count).Name = Worksheets("Business Structure").Cells(i, 17).Value
                    Next i
                        End With

Any advise would be appreciated.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about
Code:
Sub drluke()
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array("SPSJnl", "AssJnl", "VolJnl")
   For i = 0 To UBound(Ary)
      Sheets.Add(, Sheets(Sheets.Count)).Name = Ary(i)
   Next i
End Sub
 
Upvote 0
drluke,
Sorry, didn't see Fluff's response until after I had already posted the following -

Give this a try:


Code:
Sub AddSheet()
    Dim ws As Worksheet
    
    For Each Value In Array("SPSJnl", "AssJnl", "VolJnl")
    Set ws = Nothing
        On Error Resume Next
        Set ws = Worksheets(Value)
        On Error GoTo 0
        If ws Is Nothing Then
                Worksheets.Add(After:=Sheets(Sheets.Count)).Name = Value
        End If
    Next
 End Sub
 
Last edited:
Upvote 0
@Perpa
I would advise against using VBA keywords (such as Value) for variables, as it can cause unforeseen problems.
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,988
Members
448,538
Latest member
alex78

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