Copy data from all files and consolidate

boxsterman

Active Member
Joined
Apr 16, 2002
Messages
285
Hi all, I beleive I've seen something similar many times, but I can't get it to work. I have 77 files in one directory, I'd like to take the data from hidden sheet ("HC") and copy range ("A1:N21") from each file and paste it as a new table in a new master file. One sheet, data to fall in below the previous file's data.

Thank you.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Change the destination sheet name (in red) and folder path (in blue) where the files are saved, to suit your needs.
Rich (BB code):
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim wsDest As Workbook, wkbSource As Workbook
    Set wsDest = ThisWorkbook.Sheets("Sheet1")
    Const strPath As String = "C:\Test\" 'change folder path to suit your needs
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wsDest
            Sheets("HC").Range("A1:N21").Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you, but I'm getting a runtime error 13, type mismatch at this line.

Set wsDest = ThisWorkbook.Sheets("Sheet1")
 
Upvote 0
Oops!
Should be:
VBA Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim wsDest As Worksheet, wkbSource As Workbook
    Set wsDest = ThisWorkbook.Sheets("Sheet1")
    Const strPath As String = "C:\Test\" 'change folder path to suit your needs
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wsDest
            Sheets("HC").Range("A1:N21").Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,999
Messages
6,122,645
Members
449,093
Latest member
Ahmad123098

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