Using variables to name or rename worksheets

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
881
Office Version
  1. 365
Platform
  1. Windows
How can I use a variable to assign a new worksheet a new name or rename it? I have a list in col A of 200 names that need to be assigned to 200 new worksheets. Using a For Each loop with the value of each cell in col A being assigned as a new sheet name is as simple as I can desribe it. The code below is as far as I have gotten. "x" just puts the letter x in the sheet name. I want a varible routine to grab each cell value and assign it to a new worksheet. Can somebody help me???

Private Sub CommandButton2_Click()
For Each c In Worksheets("All Wells").Range("A1:A169").Cells
If c.Value = "" Then
Exit For
Else
c.Rows.Select
ActiveCell.Select
Selection.Copy
Set NewSheet = Worksheets.Add
NewSheet.Name = "x"

End If
Next
End Sub[/b]
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi Chazrab

Welcome to the Board!

Maybe:

Code:
Private Sub CommandButton2_Click() 
Dim i as integer, ws as Worksheet
With Worksheets("All Wells")
For i = 1 To .Range("A1:A169").Cells.Count
   Set ws = Sheets.Add
   ws.Name = .Cells(i,1).Value
Next
End With
End Sub
 
Upvote 0
Hello chazrab, welcome to the board.
There are a couple of things about your post that raise some questions for me.
1) You say you have a list of 200 names, yet you're only looping through 169 cells in column A.
2) Your example code tests for blanks. Does this mean the list may not reach down as far as
expected, or that there may be blanks within the list somewhere?
3) As it's written, it's a one time shot. You can't run it again to add sheets for new names added
to the list. Is this your intent?
4) What about the possibility of any duplicate names in the list. You'll run into the same problem
you would with trying to run the code more than one time.

Are any of these concerns ever going to be an issue?
 
Upvote 0
Hello chazrab,
In response to your PM, here's something you might want to give a try.
It assumes the list of sheet names is in column A of sheet 1. (Change that to suit)
It allows duplicate names and/or adding new names to the list and the code to be
run repeatedly.
It has the sleep time set for 1000 milliseconds, change that to suit as well.
Code:
Option Explicit
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub AddSheets()
Dim LstRw As Long, _
    ThisRw As Long, _
    WsList As Object, _
    Ws As Worksheet
    Set WsList = CreateObject("Scripting.Dictionary")

For Each Ws In ThisWorkbook.Worksheets
  If Not WsList.Exists(Ws.Name) Then WsList.Add Ws.Name, Nothing
Next Ws

With Sheets("Sheet1")
  LstRw = .Cells(Rows.Count, "A").End(xlUp).Row
  For ThisRw = LstRw To 1 Step -1
    If Not WsList.Exists(.Cells(ThisRw, "A").Value) Then
      Sheets.Add.Name = .Cells(ThisRw, "A").Value
      Sleep (1000)
    End If
  Next ThisRw
End With

Set WsList = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,208
Members
448,951
Latest member
jennlynn

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