Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy sheets to another workbook using vba?

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?

like image 476
Brian Avatar asked Jul 28 '11 18:07

Brian


People also ask

Can you copy data from one worksheet to another using a VBA program?

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.

How do I copy a sheet from one workbook to another?

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.

Can you copy all the worksheets to a new workbook?

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:

How do I copy a worksheet in Excel using VBA?

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.

How to copy and move a sheet in Excel?

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)

Can you copy a spreadsheet before the first sheet?

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,


8 Answers

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)

like image 116
iDevlop Avatar answered Oct 03 '22 12:10

iDevlop


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
like image 38
George Ziniewicz Avatar answered Oct 03 '22 10:10

George Ziniewicz


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.

like image 41
Chris Flynn Avatar answered Oct 03 '22 11:10

Chris Flynn


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
like image 32
Brad Avatar answered Oct 03 '22 11:10

Brad


Assuming all your macros are in modules, maybe this link will help. After copying the workbook, just iterate over each module and delete it

like image 30
raven Avatar answered Oct 03 '22 10:10

raven


Try this instead.

Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
    ws.Copy
Next
like image 30
Ch3knraz3 Avatar answered Oct 03 '22 10:10

Ch3knraz3


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.

like image 42
Hors2force Avatar answered Oct 03 '22 11:10

Hors2force


    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
like image 29
Sainath J Avatar answered Oct 03 '22 12:10

Sainath J