Fixing macro to copy worksheets

dlz217

New Member
Joined
Aug 23, 2013
Messages
49
I'm trying to create a macro that copies all worksheets from one file into an open file. XLSFile is the open file, which has the macro. I'm using the open file dialog to open FileToOpen.

I'm getting an error start at the For line saying Object Required. How can I fix this? Thanks.

Code:
Sub CopyWorkbooks()

Dim sheet As Worksheet, total As Integer


Application.ScreenUpdating = False
Application.DisplayAlerts = False


    Set XLSFile = ActiveWorkbook
    FileToOpen = Application.GetOpenFilename
If FileToOpen <> False Then
 Workbooks.Open FileToOpen
Else
 Exit Sub
End If


For Each sheet In FileToOpen.Worksheets
    total = XLSFile.Worksheets.Count
    FileToOpen.Worksheets(sheet.Name).Copy _
    after:=XLSFile.Worksheets(total)
Next sheet


End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Its because FileToOpen is just a string not an object. Try replacing a couple of your lines with:

Set wb = Workbooks.Open(FileToOpen)
For Each sheet In wb.Worksheets
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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