Naming multiple sheets at the same time

Amstrad

New Member
Joined
Feb 17, 2011
Messages
23
I want to be able to name multiple sheets in a workbook at once, rather than having to create a new sheet and rename it manually. In Sheet1, in coulumn A I have a list of names that I want each new sheet to be named. I managed to find the following code, and I have installed it as a macro:

Sub copySheet2()
Dim rngName As Range
Dim i As Integer
Set rngName = ThisWorkbook.Sheets("Sheet1").Range("a1")
Do Until rngName.Value = ""
i = ThisWorkbook.Sheets.Count
Sheets("Sheet2").Copy After:=Sheets(i)
ThisWorkbook.Sheets(i + 1).Name = rngName.Value
Set rngName = rngName.Offset(1)
Loop
End Sub

When I run it, however I get the following error:

"Can't execute code in break mode."

When I debug, it highlights Sheets("Sheet2").Copy After:=Sheets(i)

Any ideas on what is needed to fix this?

I also have a folder that contains a number of txt files. The txt files contain the data that I want to import into the new sheets I have created. In the folder I have set the default program to open the txt files as Excel.

Rather than manually copying and pasting the data from the txt file into its corresponding sheet, how can I build a macro that will import all the data all in one go.

Your help is greatly appreciated :)
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try this

Code:
Sub CopySh2()
Dim LR As Long, i As Long
With Sheets("Sheet1")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To LR
        Sheets("Sheet2").Copy after:=Sheets(Sheets.Count)
        ActiveSheet.Name = .Range("A" & i).Value
    Next i
End With
End Sub
 
Upvote 0
select the cells in A then run macro




Sub createmanysheets()
Dim ThisWS As Variant
For Each cell In Selection
ThisWS = cell.Value
Worksheets.Add after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = ThisWS
Next cell
End Sub
 
Upvote 0
select the cells in A then run macro

Sub createmanysheets()
Dim ThisWS As Variant
For Each cell In Selection
ThisWS = cell.Value
Worksheets.Add after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = ThisWS
Next cell
End Sub

Works a treat. Thank you so much.

Now I've just got to try and figure out how to merge the data from the txt files I have into each corresponding sheet ....

Any ideas would be gratefully received :)
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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