So, what I want to do, generally, is make a copy of a workbook. However, the source workbook is running my macros, and I want it to make an identical copy of itself, but without the macros. I feel like there should be a simple way to do this with VBA, but have yet to find it. I am considering copying the sheets one by one to the new workbook, which I will create. How would I do this? Is there a better way?
To open the VBA window select the Developer tab from the Excel ribbon followed by clicking on the Visual Basic Icon on the far left, alternatively the shortcut command ALT + F11 will open the VBA window. The VBA code to copy the data from one worksheet to another can now be written.
On the Edit menu, click Sheet > Move or Copy Sheet. On the To book menu, click the workbook that you want to move the sheet to. Tip: To create a new workbook that contains the moved sheet, click new book. In the Before sheet box, click the sheet that you want to insert the moved sheet before, or click move to end.
You can also think of a third scenario where you copy all the worksheets to a new workbook, but in that case, it’s more efficient to simply create a copy of the workbook instead of copying sheets. Let’s first look at how to manually copy all sheets or multiple sheets to an already open Excel workbook:
Worksheet.Copy Method in VBA First, take a look at the syntax of the Worksheet Copy method. Worksheet (<Sheet Name>).Copy (Before, After) Copy method has two arguments in it Before & After; both these are optional.
You can copy and move a sheet in Excel using a VBA code, and in this tutorial, we will learn different ways to do that. Copy a Sheet within the Same Workbook If you want to copy and sheet within the same workbook, you can use the following code where you have used the copy method. Sheets("Sheet5").Copy Before:=Sheets(1)
You can copy a spreadsheet before the first sheet, before any specific sheet, or at the end of another workbook. 1.1. Before the First Sheet of Another Workbook To copy worksheet before the first sheet of another workbook, first,
I would like to slightly rewrite keytarhero's response:
Sub CopyWorkbook()
Dim sh as Worksheet, wb as workbook
Set wb = workbooks("Target workbook")
For Each sh in workbooks("source workbook").Worksheets
sh.Copy After:=wb.Sheets(wb.sheets.count)
Next sh
End Sub
Edit: You can also build an array of sheet names and copy that at once.
Workbooks("source workbook").Worksheets(Array("sheet1","sheet2")).Copy _
After:=wb.Sheets(wb.sheets.count)
Note: copying a sheet from an XLS? to an XLS will result into an error. The opposite works fine (XLS to XLSX)
I was able to copy all the sheets in a workbook that had a vba app running, to a new workbook w/o the app macros, with:
ActiveWorkbook.Sheets.Copy
Someone over at Ozgrid answered a similar question. Basically, you just copy each sheet one at a time from Workbook1 to Workbook2.
Sub CopyWorkbook()
Dim currentSheet as Worksheet
Dim sheetIndex as Integer
sheetIndex = 1
For Each currentSheet in Worksheets
Windows("SOURCE WORKBOOK").Activate
currentSheet.Select
currentSheet.Copy Before:=Workbooks("TARGET WORKBOOK").Sheets(sheetIndex)
sheetIndex = sheetIndex + 1
Next currentSheet
End Sub
Disclaimer: I haven't tried this code out and instead just adopted the linked example to your problem. If nothing else, it should lead you towards your intended solution.
You could saveAs xlsx. Then you will loose the macros and generate a new workbook with a little less work.
ThisWorkbook.saveas Filename:=NewFileNameWithPath, Format:=xlOpenXMLWorkbook
Assuming all your macros are in modules, maybe this link will help. After copying the workbook, just iterate over each module and delete it
Try this instead.
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Copy
Next
You can simply write
Worksheets.Copy
in lieu of running a cycle. By default the worksheet collection is reproduced in a new workbook.
It is proven to function in 2010 version of XL.
Workbooks.Open Filename:="Path(Ex: C:\Reports\ClientWiseReport.xls)"ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With