VBS Script copy active sheet to two sheets with defined names

ceytl

Board Regular
Joined
Jun 6, 2009
Messages
114
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have been trying to write a VBA script to take the active sheet and copy it to two sheets that are name V001 and V2Y

When I run the following error it says the name already exist, and I need to rename it.

Do you know what i am doing wrong?

Sub Add_Sheet()
Copy1
Copy2
End Sub
Private Sub Copy1()

ActiveSheet.Copy After:=Worksheets(Sheets.Count)
On Error Resume Next
ActiveSheet.Name = "V001"

End Sub
Private Sub Copy2()

Sheets("V001").Copy After:=Sheets(Sheets.Count)
On Error Resume Next
ActiveSheet.Name = "V2Y"
On Error GoTo 0

End Sub

Thanks!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
How about:

VBA Code:
Sub CopyCurrentSheetToTwoDifferentSheets()
'
    Dim CopiesCounter   As Long
    Dim TotalCopies     As Long
    Dim xName           As String
    Dim WS              As Worksheet
'
    Application.ScreenUpdating = False
'
    Set WS = ActiveSheet                        ' <--- Set this to the sheet that you want to copy
'
    On Error Resume Next
'
    Application.DisplayAlerts = False
    Sheets("V001").Delete
    Sheets("V2Y").Delete
    Application.DisplayAlerts = True
'
    WS.Copy after:=Worksheets(Sheets.Count)
    ActiveSheet.Name = "V001"
'
    WS.Copy after:=Worksheets(Sheets.Count)
    ActiveSheet.Name = "V2Y"
'
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
How about:

VBA Code:
Sub CopyCurrentSheetToTwoDifferentSheets()
'
    Dim CopiesCounter   As Long
    Dim TotalCopies     As Long
    Dim xName           As String
    Dim WS              As Worksheet
'
    Application.ScreenUpdating = False
'
    Set WS = ActiveSheet                        ' <--- Set this to the sheet that you want to copy
'
    On Error Resume Next
'
    Application.DisplayAlerts = False
    Sheets("V001").Delete
    Sheets("V2Y").Delete
    Application.DisplayAlerts = True
'
    WS.Copy after:=Worksheets(Sheets.Count)
    ActiveSheet.Name = "V001"
'
    WS.Copy after:=Worksheets(Sheets.Count)
    ActiveSheet.Name = "V2Y"
'
    Application.ScreenUpdating = True
End Sub

Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,984
Members
449,058
Latest member
oculus

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