vba to copy one sheet to another sheet

BORUCH

Well-known Member
Joined
Mar 1, 2016
Messages
528
Office Version
  1. 365
Platform
  1. Windows
Hi all

I'm having this problem with the below vba code

VBA Code:
 Sub CopySheetFromClosedWorkbook()
    Dim sourceBook As Workbook
    Application.ScreenUpdating = False
    Set sourceBook = Workbooks.Open("C:\Users\jack\Documents\abcd\Sales invoice.xlsx")
    With Workbooks(sourceBook.Name)
    .Sheets("aaa").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    sourceBook.Close
    Application.ScreenUpdating = True
end with
End Sub

I'm getting error "copy method of worksheet class failed"

it might have to do something with my personal.xlsb file I'm not sure

any help is appreciated

thanks
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi,​

as it obviously depends on which codeline the error occurs but you forgot to mention it !​
Anyway removing the useless :​
VBA Code:
Sub CopySheetFromClosedWorkbook()
        Application.ScreenUpdating = False
    With Workbooks.Open("C:\Users\jack\Documents\abcd\Sales invoice.xlsx")
        .Sheets("aaa").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
        .Close False
    End With
        Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi,​
as it obviously depends on which codeline the error occurs but you forgot to mention it !​
Anyway removing the useless :​
VBA Code:
Sub CopySheetFromClosedWorkbook()
        Application.ScreenUpdating = False
    With Workbooks.Open("C:\Users\jack\Documents\abcd\Sales invoice.xlsx")
        .Sheets("aaa").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
        .Close False
    End With
        Application.ScreenUpdating = True
End Sub

The error occurs on this line
VBA Code:
.Sheets("aaa").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)

and I'm still having the same issue
 
Upvote 0
So the source sheet does not exist - check its name - or for some reason the issue is on destination workbook side so try manually​
and if it works activate the Macro Recorder and redo the same operation in order to compare with the generated code …​
 
Upvote 0
So the source sheet does not exist - check its name - or for some reason the issue is on destination workbook side so try manually​
and if it works activate the Macro Recorder and redo the same operation in order to compare with the generated code …​
this is how the macro recorder does it
Workbooks.Open Filename:= _ "C:\Users\jack\Documents\abcd\Sales invoice.xlsx" Sheets("Roll Out Summary").Select Sheets("Roll Out Summary").Copy After:=Workbooks("wwww.xlsx").Sheets( _ 1) ActiveWindow.Close End Sub

THE problem is i don't want to have to specify the sheet name it can always be a different sheet name so it works like this but it wouldn't be efficient to me
 
Upvote 0
So it was your bad as the sheet did not exist ‼​
Now you must elaborate your strategy to copy some sheet without it's name …​
 
Upvote 0
So it was your bad as the sheet did not exist ‼​
Now you must elaborate your strategy to copy some sheet without it's name …​
ok my goal is to open a workbook in a certain directory and copy the sheet named "Roll Out Summary" to the other active workbook that i originally opened and then close the other workbook where it copied it form
Thanks
 
Upvote 0
So obviously just update the sheet name in the post #2 VBA procedure …​
 
Upvote 0
Or use your post #5 code (without the Select codeline) …​
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,965
Members
449,201
Latest member
Jamil ahmed

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