Macro to Rename worksheet tabs

Sallyprit

New Member
Joined
Jan 25, 2024
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi I copied the following code from a post on this site which works beautifully. I now need to rename each new tab to cell B2 of that new worksheet. If anyone can help I'd really appreciate it. Thanks Sally 😊😊
Sub copycols()
Dim LC As Long, i As Long, ws As Worksheet
With ActiveSheet
LC = .Cells(1, Columns.Count).End(xlToLeft).Column
For i = 2 To LC
Set ws = Sheets.Add(after:=Sheets(Sheets.Count))
.Columns(1).Copy Destination:=ws.Range("A1")
.Columns(i).Copy Destination:=ws.Range("B1")
Next i
End With
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Welcome to the Board!

Try inserting the line in red
Rich (BB code):
Set ws = Sheets.Add(after:=Sheets(Sheets.Count))
ws.Name = ws.Range("B2")
 
Upvote 0
Welcome to the Board!

Try inserting the line in red
Rich (BB code):
Set ws = Sheets.Add(after:=Sheets(Sheets.Count))
ws.Name = ws.Range("B2")
Thanks Joe 😊 so I've made the code as below bur I now get a run time error. Have I put the red line in the wrongplace maybe? Really appreciate your help.

Sub copycols()
Dim LC As Long, i As Long, ws As Worksheet
With ActiveSheet
LC = .Cells(1, Columns.Count).End(xlToLeft).Column
For i = 2 To LC
Set ws = Sheets.Add(after:=Sheets(Sheets.Count))
ws.Name = ws.Range("B2")
.Columns(1).Copy Destination:=ws.Range("A1")
.Columns(i).Copy Destination:=ws.Range("B1")
Next i
End With
End Sub

1706189642203.png
 
Upvote 0
Whoops, I just noticed that the two rows of code below that are what is populating those columns. So it probably needs to be after them, i.e.
Rich (BB code):
.Columns(1).Copy Destination:=ws.Range("A1")
.Columns(i).Copy Destination:=ws.Range("B1")
ws.Name = ws.Range("B2")

Sorry about that!
 
Upvote 0
Solution
Thanks Joe 😊 so I've made the code as below bur I now get a run time error. Have I put the red line in the wrongplace maybe? Really appreciate your help.

Sub copycols()
Dim LC As Long, i As Long, ws As Worksheet
With ActiveSheet
LC = .Cells(1, Columns.Count).End(xlToLeft).Column
For i = 2 To LC
Set ws = Sheets.Add(after:=Sheets(Sheets.Count))
ws.Name = ws.Range("B2")
.Columns(1).Copy Destination:=ws.Range("A1")
.Columns(i).Copy Destination:=ws.Range("B1")
Next i
End With
End Sub

View attachment 105697
I have now moved the red code down to make it

Sub copycols()
Dim LC As Long, i As Long, ws As Worksheet
With ActiveSheet
LC = .Cells(1, Columns.Count).End(xlToLeft).Column
For i = 2 To LC
Set ws = Sheets.Add(after:=Sheets(Sheets.Count))
.Columns(1).Copy Destination:=ws.Range("A1")
.Columns(i).Copy Destination:=ws.Range("B1")
ws.Name = ws.Range("B1")
Next i
End With
End Sub

it works perfectly, thank you.
 
Upvote 0
Whoops, I just noticed that the two rows of code below that are what is populating those columns. So it probably needs to be after them, i.e.
Rich (BB code):
.Columns(1).Copy Destination:=ws.Range("A1")
.Columns(i).Copy Destination:=ws.Range("B1")
ws.Name = ws.Range("B2")

Sorry about that!
Its no problem at all. I managed to work it out!
 
Upvote 0
You are welcome!
Glad I was able to help.

Note when marking a post as the solution, please mark the original post that contains the working solution (and not you own post acknowledging that another post was the solution).
I have updated it for you on this thread.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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