Macro for copying and pasting workbooks into new sheets

elayem

Board Regular
Joined
Sep 11, 2009
Messages
51
Hi,

Please help with this problem. I tried searching other posts for answers as well.

I have a main workbook, Main.xlsm

I want to have a macro in there that can loop through and import data from whatever xls files are in a specific folder into new worksheets. The folder is "C:\Users\elayem\Downloads\Import"

For example if I have 3 workbooks in there:
workbook1.xls
workbook2.xls
workbook3.xls

The macro would import each workbook into 3 new worksheets in Main.xlsm.
Order does not matter. Naming of the new worksheets does not matter.

Can anyone help with this script? Thank you in advance!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
But, in case you don't have Power Query, here's a macro for Main.xlsm you can try. Untested, so may need some tweaking. Assumes you have only one sheet to be copied from the imported workbooks, and that sheet is active when the workbook is opened, and the imported sheets don't all have the same tab name.
Code:
Sub elayem()
Dim pStr As String, myFile As String
pStr = "C:\Users\elayem\Downloads\Import\"
myFile = Dir(pStr & "*xls")
If myFile = vbNullString Then
    Exit Sub
Else
       Workbooks.Open pStr & myFile
       With ActiveSheet
              .Move after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
              .Parent.Close savechanges:=False
       End With
End If
Do
       myFile = Dir()
       If myFile = vbNullString Then
              Exit Do
       Else
              Workbooks.Open pStr & myFile
              With ActiveSheet
                     .Move after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
                     .Parent.Close savechanges:=False
              End With
       End If
Loop
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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