+ Reply to Thread
Results 1 to 28 of 28

Excel 2007 : Combining data from multiple Excel Files

Hybrid View

  1. #1
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Combining data from multiple Excel Files

    Hi robbyvegas,
    This will be the basic code that can be expanded on to copy all the sheets in the workbooks
    Sub Consolidate()
    'Author:     Jerry Beaucaire'
    'Date:       9/15/2009     (2007 compatible)
    'Summary:    Open all Excel files in a specific folder and merge data
    '            into one master sheet (stacked)
    '            Moves imported files into another folder
    Dim fName As String, fPath As String, fPathDone As String, OldDir As String
    Dim LR As Long, NR As Long
    Dim wbData As Workbook, wbkNew As Workbook
    
    'Setup
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.DisplayAlerts = False
        
        Set wbkNew = ThisWorkbook
        wbkNew.Activate
        Sheets("Master").Activate   'sheet report is built into
        
        If MsgBox("Import new data to this report?", vbYesNo) = vbNo Then Exit Sub
        
        If MsgBox("Clear the old data first?", vbYesNo) = vbYes Then
            Cells.Clear
            NR = 1
        Else
            NR = Range("A" & Rows.Count).End(xlUp).Row + 1
        End If
    
    'Path and filename (edit this section to suit)
        fPath = "C:\2010\"                  'remember final \ in this string
        fPathDone = fPath & "Imported\"     'remember final \ in this string
        On Error Resume Next
            MkDir fPathDone                 'creates the completed folder if missing
        On Error GoTo 0
        OldDir = CurDir                     'memorizes the users current working path
        ChDir fPath                         'activate the filepath with files to import
        fName = Dir("*.xls")                'listing of desired files, edit filter as desired
    
    'Import a sheet from found file
        Do While Len(fName) > 0
            If fName <> wbkNew.Name Then     'make sure this file isn't accidentally reopened
            'Open file
                Set wbData = Workbooks.Open(fName)
    
            'This is the section to customize, replace with your own action code as needed
            'Find last row and copy data
                LR = Range("A" & Rows.Count).End(xlUp).Row
                If NR = 1 Then      'copy the titles and data
                    Range("A1:A" & LR).EntireRow.Copy _
                        wbknew.Sheets("Master").Range("A" & NR)
                Else                'copy the data only
                    Range("A2:A" & LR).EntireRow.Copy _
                        wbknew.Sheets("Master").Range("A" & NR)
                End If
            'close file
                wbData.Close False
            'Next row
                NR = Range("A" & Rows.Count).End(xlUp).Row + 1
            'move file to IMPORTED folder
                Name fPath & fName As fPathDone & fName
            'ready next filename
                fName = Dir
            End If
        Loop
    
    ErrorExit:    'Cleanup
        ActiveSheet.Columns.AutoFit
        Application.DisplayAlerts = True         'turn system alerts back on
        Application.EnableEvents = True          'turn other macros back on
        Application.ScreenUpdating = True        'refreshes the screen
        ChDir OldDir                             'restores users original working path
    End Sub
    "IS ALWAYS the same" is had to work with. sample workbook ect.. and expanding what is required would help. read jb instructions for the code and Other than that its a help forum and ask questions.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  2. #2
    Registered User
    Join Date
    02-11-2010
    Location
    Maryland, USA
    MS-Off Ver
    Excel 2007
    Posts
    83

    Question Re: Combining data from multiple Excel Files

    Thanks again Pike,

    One question that I have when I open up the VBA editor, is where to insert the code that you copied - "where" meaning, do I create a new module? If so, where should the module be located, etc.

    I wish that I could provide someone out there a copy of the data that I'm working with, but I'm sure you are all used to data in excel spreadsheets being sensitive. Consequently, I've attached "sample.xls" from which I've stripped of all underlying data. As you can see, There are weekly copies of identical spreadsheets. The workbooks contain more than 1 worksheet, but the sheet that I want to use from each workbook is always the first sheet in the workbook.

    I've also attached "sample2.xlsx" to demonstrate what data I'd like to glean from the Sample.xls spreadsheet - what I envision it looking like when it works properly. Where there are formulas, the formula is the actual formula that exists in the spreadsheets. In all other circumstances I've simply inserted filler data so that you can get a picture as to what the spreadsheet is supposed to look like.

    I expect that up until now, it is pretty straight forward (although I really don't know). I'll throw a small kink into the works. First, it is possible that there could be more than 3 levels (e.g. a copy of level 3 with two different prices) which potentially adds an extra row. Also, the titles (blue cells) shown in columns L-X may change (e.g. if "Center" is YYYY instead of XXXX, or for other reasons). Nonetheless, for columns A-J, the column titles will always stay the same, and for columns L-X, as you can tell from the 'Count' spreadsheet of Sample2, it is OK to simply copy the title of the column (The single row directly above the numerical data) into the "Type" column. The problem is that there could be a greater or fewer number of "Types" than what is shown in Sample.xls (it could run from A-M and F1-J2 instead of A-E and F1-G8 as shown).

    I believe that is an accurate run down of all of the work that I have before me. Hopefully you guys have a kind heart and are willing to spend a little bit of time helping me out with this.

    Thank you,
    Rob
    Attached Files Attached Files

+ Reply to Thread

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