Results 1 to 1 of 1

Appending many different xls files with the same headers to one master file

Threaded View

  1. #1
    Registered User
    Join Date
    05-16-2013
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010
    Posts
    1

    Appending many different xls files with the same headers to one master file

    Hello everyone,

    I'm sure my problem is quite simple, and I've found many threads requesting similar things, but somehow none of them seem to work for me.

    Here is what I want to do: loop through a folder, grabbing all the xls files with a specific string contained in them, and then put them all end to end inside of a master excel file.

    As of right now, I'm ignoring the "specific string" bit in the hopes of just getting something working. All my files have all their data on Sheet1, and all have a header row in row 1 and data starting in row 2. Files of the same type have identical header rows, and can have missing values in many columns (some are completely empty).

    My attempt so far:

    Sub MergeSheets()
        Dim SrcBook As Workbook, Master As Workbook
        Dim oFSO As Object, Folder As Object, Files As Object, file As Object
           
        Application.ScreenUpdating = False
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        Set Folder = oFSO.Getfolder("C:\Temp\")
        Set Files = Folder.Files
        
        Set Master = Workbooks.Add
        
        For Each file In Files
            Set SrcBook = Workbooks.Open(file)
            Range("A2:CT65536").Copy
            Master.Worksheets("Sheet1").Activate
            Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
            Application.CutCopyMode = False
            SrcBook.Close
        Next
    End Sub
    This seems to copy everything on top of the previously copied files, so the contents of the last file is the only thing present in the finished product. I'm also not sure how to get the header row across.

    Out of all the different sheet categories, none have greater breadth than CT, hence I used that variable. The number of observations is variable though.

    EDIT:

    I thought that the line

    Range("A2:CT65536").Copy
    Was the problem, so I tried switching it with

    Range("A1:CT" & Range("A65536").End(xlUp).Row).Copy
    but that just gives me the headers. The very first column can be completely blank, so I guess that is the issue.

    I'm trying to get a handle on how range really works because I obviously don't understand it, but I haven't really found a good resource. The help page isn't that helpful. Any suggestions of somewhere to read up?

    EDIT2:

    I ran the following code:

    For Each file In Files
            Set SrcBook = Workbooks.Open(file)
            Range("A1:CT" & Range("B65536").End(xlUp).Row).Copy
            Master.Worksheets("Sheet1").Activate
            Range("B65536").End(xlUp).Offset(1, -1).PasteSpecial
            Application.CutCopyMode = False
            SrcBook.Close
        Next
    where column B is filled for every row in this specific worksheet category. Now I get all the data, however it starts on the second row and a header row gets pasted for each import.
    Last edited by setsanto; 05-16-2013 at 10:24 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1