Copy Worksheets

Papi

Well-known Member
Joined
May 22, 2007
Messages
1,592
This code works fine to create worksheets but it cannot be ran twice in a row. What I would like to have it do is ask how many copies are needed and be able do this when requested and go with one number higher than the last worksheet, which is always on the right. Can somebody see what is stopping it from doing that? If the existing worksheets are Report 156 then Report 157, the next two would be Report 158 and Report 159 etc.

Code:
Sub Copy_Sheets()
   Application.ScreenUpdating = False
   Application.DisplayAlerts = False
   If ActiveSheet.Name <> "Report" Then
   MsgBox "You must be in ''Report'' to run this macro."
   Exit Sub
 End If
    Sheets("Report").Select
    Dim x As Integer
    Dim i As Integer
    Dim CurSheet As String
    CurSheet = ActiveSheet.Name
    i = Application.InputBox("How many sheets are needed?", "Add Sheets", Type:=1)
    Do While x < i
    ActiveSheet.Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Report " & x + 1
    x = x + 1
    Sheets(CurSheet).Activate
Loop
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi Papi,

I think that u need to get the latest # from the exiting workshees first.

Code:
Sub Copy_Sheets_colo()
    Const sCurshName As String = "Report"
    Dim lLastNum As Long
    Dim lWksNeed As Long
    Dim i As Long
    
    Application.ScreenUpdating = False
    
    'Get latest number
    lLastNum = Trim(Mid(Sheets(Sheets.Count).Name, Len(sCurshName) + 1))
    lngWksNeed = Application.InputBox("How many sheets are needed?", "Add Sheets", Type:=1)
    
    'Canceled
    If lngWksNeed = False Then Exit Sub
    
    For i = 1 To lngWksNeed
        Sheets(sCurshName).Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = sCurshName & lLastNum + 1
        lLastNum = lLastNum + 1
    Next
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,186
Members
449,071
Latest member
cdnMech

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