Sequential Sheets Issue

asjmoron

Board Regular
Joined
Apr 26, 2016
Messages
98
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys.

I have workbook that has numbered sheets in the format 001|002|003 etc...

I am trying write some VBA to duplicate the last sheet, formulas included, and continue the numbering of the sheets in the same format.

For example, the user has 30 sheets (001 - 030) and adds a new sheet. the code will duplicate 030 and create a new sheet named 031 with all forumlas included.

So far I have managed to do the duplication but I can not get the naming of the sheet down!

Any help would be great!

Below is the current code I have...…..

Code:
Sub Create()
    Dim I As Long
    Dim xNumber As Integer
    Dim xName As String
    Dim xActiveSheet As Worksheet
    On Error Resume Next
    Application.ScreenUpdating = False
    Set xActiveSheet = ActiveSheet
    xNumber = InputBox("How Many More sheets do you need?")
    For I = 1 To xNumber
        xName = ActiveSheet.Name
        xActiveSheet.Copy After:=ActiveWorkbook.Sheets(xName)
        ActiveSheet.Name = ActiveSheet.Name & I
    Next
    xActiveSheet.Activate
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Could you get the name of the last sheet and then convert it to a number so you can add 1 to the value?

Code:
Worksheets(Worksheets.Count).Select
NewSheetname = Val(ActiveSheet.Name) + 1

Can you work with that?
 
Upvote 0
Probably a better way but...

Code:
Sub Create()
    Dim I As Long
    Dim xNumber As Integer
    Dim xName As String
    Dim xActiveSheet As Worksheet
    On Error Resume Next
    Application.ScreenUpdating = False
    Set xActiveSheet = ActiveSheet
    xNumber = InputBox("How Many More sheets do you need?")
    I = 1
    Do Until I = xNumber + 1
        xName = ActiveSheet.Name
        xActiveSheet.Copy After:=ActiveWorkbook.Sheets(xName)
        ActiveSheet.Name = Format(CLng(xActiveSheet.Name) + I, "000")
    I = I + 1
    Loop
    xActiveSheet.Activate
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Replace this :
Code:
ActiveSheet.Name = ActiveSheet.Name & I
with this:
Code:
ActiveSheet.Name = Format(xName + 1, "000")
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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