Copying Entire Worksheets into a new workbook with VBA- Help

sll810

Board Regular
Joined
Jun 29, 2007
Messages
86
I am writing VBA in access controlling Excel. I need to open a new workbook and copy the first sheet, called Summary into a new sheet in the new workbook and then save the new workbook. I keep getting an error saying "Copy method of Worksheet class failed"

Here is my copy code below:

Set xlapp = New Excel.Application
xlapp.Visible = True
Set reportbook = xlapp.Workbooks.Add
scrapbook.Worksheets("Summary").Copy_
Before:=reportbook.Worksheets(1)

I have tried tio use the name of a worksheet also when referencing the reportbook.

Any ideas are MUCH appreciated.

Thanks!!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I may be wrong but it sounds like you are dealing with two workbooks (the new one you added and another you are copying a sheet from).

Be sure you fully reference the workbooks that you copy from and copy to:

This sub copies a sheet called "Sheet1" from Book1.xls and places it before the first sheet in Book2.xls.

Code:
Sub test()
    
    Workbooks("Book1.xls").Activate
    Workbooks("Book1.xls").Sheets("Sheet1").Copy _
        Before:=Workbooks("Book2.xls").Worksheets(1)
    
    End Sub

A further issue may arise if you are creating a new instance of Excel - does it recognize workbooks open in another instance? Not sure here...

HTH
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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