Automatically rename copied sheet -VBA

PATSYS

Well-known Member
Joined
Mar 12, 2006
Messages
1,750
Hi all,

I have the following code:

Code:
Sub Copysheet()
    
Dim i As Integer

i = 1
    
For i = 1 To 8
    
    Sheets("P&L-OPS").Copy Before:=Sheets(5)

Next i

End Sub

How can I include a line/lines that will rename the copied 8 sheets as follows:

MAN
MAK
PAK
MAP
PAR
KAR
RAK
KAN

In that exact order?

Thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Copysheet()<br><br>    <SPAN style="color:#00007F">Dim</SPAN> i      <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> 8<br>        Sheets("P&L-OPS").Copy Before:=Sheets(5)<br>        Sheets(5).Name = Array("MAN", "MAK", "PAK", "MAP", "PAR", "KAR", "RAK", "KAN")(8 - i)<br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Thanks again Alphafrog.

I hope this will be the last question I will be asking for this weekend :)
 
Upvote 0
Or slightly different:

Code:
Sub Copysheet()
    Dim i As Integer
    Application.ScreenUpdating = False
    For i = 1 To 8
        Sheets("P&L-OPS").Copy Sheets(5)
        ActiveSheet.Name = Choose(i, "MAN", "MAK", "PAK", "MAP", "PAR", "KAR", "RAK", "KAN")
    Next
End Sub
 
Upvote 0
Hello wigi,

Your solution results in the sheets named in reverse order. You would have to reverse sheet names in the choose function or use something like this...

<font face=Courier New>ActiveSheet.Name = Choose(9 - i, "MAN", "MAK", "PAK", "MAP", "PAR", "KAR", "RAK", "KAN")</FONT>

Or use
<font face=Courier New>    <SPAN style="color:#00007F">For</SPAN> i = 8 <SPAN style="color:#00007F">To</SPAN> 1 <SPAN style="color:#00007F">Step</SPAN> -1</FONT>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,894
Members
452,948
Latest member
Dupuhini

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