Macro to copy tab from closed workbook not working

_eNVy_

Board Regular
Joined
Feb 9, 2018
Messages
66
Office Version
  1. 365
Platform
  1. Windows
Hi all,

The below code doesn't seem to be working:

VBA Code:
Sub CopySheet()

Application.ScreenUpdating = False
 
    Set closedBook = Workbooks.Open("C:\Users\040429\Documents\Workings\Excel\_Holidays_.xlsb")
    closedBook.Sheets("Holidays ").Copy Before:=ThisWorkbook.ActiveSheet
    closedBook.Close SaveChanges:=False
 
Application.ScreenUpdating = True
End Sub

I want to open up the Holidays file and copy "Holidays " tab and paste into the Active Workbook from which I initiated the macro.
However I get an message : Run-time error '1004'
Copy method of Worksheet class failed.

Help on this would be really helpful!
Thanks!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try:
VBA Code:
Sub CopySheet()
    Application.ScreenUpdating = False
    Dim desWB As Workbook
    Set desWB = ThisWorkbook
    Set closedBook = Workbooks.Open("C:\Users\040429\Documents\Workings\Excel\_Holidays_.xlsb")
    closedBook.Sheets("Holidays ").Copy Before:=desWB.ActiveSheet
    closedBook.Close SaveChanges:=False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub CopySheet()
    Application.ScreenUpdating = False
    Dim desWB As Workbook
    Set desWB = ThisWorkbook
    Set closedBook = Workbooks.Open("C:\Users\040429\Documents\Workings\Excel\_Holidays_.xlsb")
    closedBook.Sheets("Holidays ").Copy Before:=desWB.ActiveSheet
    closedBook.Close SaveChanges:=False
    Application.ScreenUpdating = True
End Sub
Thank you for your swift response. I am still getting an error sadly.

The code highlighted is

VBA Code:
closedBook.Sheets("Holidays ").Copy Before:=desWB.ActiveSheet
 
Upvote 0
Do you have a sheet named "Holidays" in the closed workbook?
 
Upvote 0
As far a I can see the code should work. Could you upload a copy of your Holidays_.xlsb file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Hi mumps - the link to the file is here:


Thank you for your help!
 
Upvote 0
I tested the macro and it worked properly. After you run the macro, check to see if the "_Holidays_.xlsb" file has been opened by the macro before it generates the error.
 
Upvote 0
I tested the macro and it worked properly. After you run the macro, check to see if the "_Holidays_.xlsb" file has been opened by the macro before it generates the error.

Hi mumps - apologies for the late reply.
Running the code line by line, it does open the "_Holidays_.xlsb" file and still errors on :

VBA Code:
    closedBook.Sheets("Holidays ").Copy Before:=desWB.ActiveSheet
 
Upvote 0
This has been fixed!!!!

I changed the properties of desWB from ThisWorkbook to ActiveWorkbook.
This has solved all my problems.

Thank you mumps for your help, would not have gotten to where I am without it.

Take care!
 
Upvote 0
Solution

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

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