Using VBA to make a copy of the Active Worksheet

Graebeard

New Member
Joined
Mar 21, 2015
Messages
23
Office Version
  1. 2019
Platform
  1. Windows
This driving me insane.. :(

I want to make a copy of the current worksheet into the same workbook and assign a new name to it. Basically, I have a Raw Data worksheet, which I want to duplicate so it can be processed into a final layout. All the other code works fine, I just need to automate the monthly copying of the Raw Data sheets.

Here is the code I've pulled off this and other Forums. Unfortunately, I get an Error 9 when I run it.
---------------------------------------------------------------------------------------------
Sub macro3()

ActiveWorkbook.ActiveSheet.Copy Before:=ActiveWorkbook.Sheets("sheet5")

End Sub
---------------------------------------------------------------------------------------------

I have only one workbook open and that one has 8 named Sheets. Sheet5 is just one of several I've tried with the same results. It turns out that if I don't use the Before:= or After:= qualifier, the sheet is copied into a new workbook.

Any suggestions would be greatly appreciated

Thanks in Advance
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi,

Try it like this. This copies the active worksheet to the end of the workbook and renames with the name of the active sheet and Copy


Sub somesub()
Dim ShName As String

With ActiveSheet
ShName = .Name & "Copy"
.Copy After:=Sheets(Worksheets.Count)
End With
Sheets(Worksheets.Count).Name = ShName
End Sub
 
Upvote 0
Perfect.

Thanks Mike. That did the trick and I'll use it. Don't understand why the other code errored out.


Graebeard (y)
 
Upvote 0

Forum statistics

Threads
1,214,627
Messages
6,120,610
Members
448,973
Latest member
ChristineC

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