MACRO help to repeat copy paste save

davin2929

Board Regular
Joined
Oct 13, 2002
Messages
129
Hi,

Does anyone know if it's possible to open each file in a folder and copy a worksheet in each of the files? The names of the files vary but are located within one folder. I would like to grab a worksheet named "Sales Summary" out of each of the .xls files within the folder.

So if there were 12 files in a folder I would like to grab 12 Sales Summary worksheets, 1 out of each of the files.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Sure.

I am actually helping someone who is opening all the Excel files from a certain directory (http://www.mrexcel.com/forum/showthread.php?t=580800).

Starting from something like that, we can build this to do what you want:
Code:
Sub MyCopySheets()
 
    Dim wbOrig As Workbook
    Dim wbOpen As Workbook
    Dim strPath As String
    Dim strExtension As String
    
    Application.ScreenUpdating = False
 
'   Capture current workbook
    Set wbOrig = ActiveWorkbook
 
'   Set path to open Excel files from
    strPath = "G:\C\Temp\"
    
    ChDrive Left(strPath, 3)
    ChDir strPath
    strExtension = Dir("*.xls*")
 
'   Loop through all files in directory
    Do While strExtension <> ""
'   Open workbook
        Set wbOpen = Workbooks.Open(strPath & strExtension)
        wbOpen.Sheets("Sales Summary").Activate
        Cells.Copy
'   Insert new worksheet and paste
        wbOrig.Activate
        Sheets.Add After:=Sheets(Sheets.Count)
        Range("A1").Activate
        ActiveSheet.Paste
        Application.CutCopyMode = False
'   Go back and close file that we copied from
        wbOpen.Close SaveChanges:=False
        strExtension = Dir
    Loop
 
    Application.ScreenUpdating = True
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,293
Members
452,902
Latest member
Knuddeluff

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