VBA Code to Name New Worksheets in Increments of 1

leonalap24

New Member
Joined
Jan 11, 2015
Messages
11
I have a main worksheet (Test) that I copy each day and I would l would like it to automatically be renamed Test 1, Test 2 and so on when I make a copy. I am really new to VBA and could use some help. Thanks in advance.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Place this macro in the workbook containing the Sheet "Test". Then run it each time you want to create and name a new sheet based on Test, starting with "Test 1".

To install the code:
1. With your workbook active press Alt and F11 keys. This will open the VBE window.
2. In the project tree on the left of the VBE window, find your project and click on it.
3. On the VBE menu: Insert>Module
4. Copy the code from your browser window and paste it into the white space in the VBE window.
5. Close the VBE window and Save the workbook. If you are using Excel 2007 or a later version do a SaveAs and save it as a macro-enabled workbook (.xlsm file extension).
6. Press Alt+F8 keys to run the code
7. Make sure you have enabled macros whenever you open the file or the code will not run.
Code:
Sub ConsecutiveNumberSheets()
With Sheets("Test")
    .Copy after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Test" & Sheets.Count - 1
    .Select
End With
End Sub
 
Upvote 0
Place this macro in the workbook containing the Sheet "Test". Then run it each time you want to create and name a new sheet based on Test, starting with "Test 1".

To install the code:
1. With your workbook active press Alt and F11 keys. This will open the VBE window.
2. In the project tree on the left of the VBE window, find your project and click on it.
3. On the VBE menu: Insert>Module
4. Copy the code from your browser window and paste it into the white space in the VBE window.
5. Close the VBE window and Save the workbook. If you are using Excel 2007 or a later version do a SaveAs and save it as a macro-enabled workbook (.xlsm file extension).
6. Press Alt+F8 keys to run the code
7. Make sure you have enabled macros whenever you open the file or the code will not run.
Code:
Sub ConsecutiveNumberSheets()
With Sheets("Test")
    .Copy after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Test" & Sheets.Count - 1
    .Select
End With
End Sub

That worked but is there any way for there to be a space between test and the number? It is coming up as Test1 and I would like it to be Test 1. Thanks for your help.
 
Upvote 0
Please use the below one..



Sub ConsecutiveNumberSheets()With Sheets("Test") .Copy after:=Sheets(Sheets.Count) ActiveSheet.Name = "Test " & Sheets.Count - 1 .SelectEnd WithEnd Sub</pre>
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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