Copy worksheets from one workbook to another

panman1

New Member
Joined
Jul 31, 2019
Messages
2
I know very similar questions have previously been asked and I have tried to follow them without success. When I run this I get a Run-time error '1004' msg.

The 'CopySheets' Macro is saved in the 'ToWorkbook'

Thanks


Sub CopySheets()

Dim FromWorkbook As Workbook
Dim ToWorkbook As Workbook

Set FromWorkbook = Workbooks("FromWorkbook.xlsm")
Set ToWorkbook = Workbooks("ToWorkBook.xlsm")

Dim FromSheet As Worksheet

For Each FromSheet In FromWorkbook.Sheets
FromSheet.Copy After:=ToWorkbook.Sheets(ToWorkbook.Sheets.Count)
Next
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
What is the error message?
 
Upvote 0
Welcome to the forum!

Do you have any of the books protected, mainly ToWorkbook?
You have other macros in the sheets, it may be necessary to disable events. Try this:

Code:
Sub CopySheets()
  Dim FromWorkbook As Workbook
  Dim ToWorkbook As Workbook
  Dim FromSheet As Worksheet
  Set FromWorkbook = Workbooks("FromWorkbook.xlsm")
  Set ToWorkbook = Workbooks("ToWorkBook.xlsm")
[COLOR=#0000ff]  Application.EnableEvents = False[/COLOR]
  For Each FromSheet In FromWorkbook.Sheets
    FromSheet.Copy After:=ToWorkbook.Sheets(ToWorkbook.Sheets.Count)
  Next
[COLOR=#0000ff]  Application.EnableEvents = True[/COLOR]
End Sub


Another option to copy all sheets.
Code:
Sub CopySheets()
  Dim ToWorkbook As Workbook
  Set ToWorkbook = Workbooks("ToWorkBook.xlsm")
  Application.EnableEvents = False
  Workbooks("FromWorkbook.xlsm").Worksheets.Copy After:=ToWorkbook.Sheets(ToWorkbook.Sheets.Count)
  Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,980
Members
449,201
Latest member
Lunzwe73

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