+ Reply to Thread
Results 1 to 28 of 28

Excel 2007 : Combining data from multiple Excel Files

Hybrid View

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

    Question Combining data from multiple Excel Files

    Here is the question...

    I have select data entries in multiple (=52*7) excel spreadsheets (separate files) that I need to collect into 1 spreadsheet. The format of the source table is almost always the same, and the title row of the column that I need to retrieve data from IS ALWAYS the same. Is there a way to save myself some time here, or am I destined to spend the next few hours in copy-paste world?

    Thanks,
    Rob

  2. #2
    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

    Hey robbyvegas
    No, JB has some samples here to get you started
    https://sites.google.com/a/madrocket...ssistant/files say good bye to copy and paste world
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

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

    Re: Combining data from multiple Excel Files

    Sweet. Thanks. Now, while I'm not a noob to programming generally, I am a noob to programming VBA scripts in Excel. Any advice on where to go to get a quick primer on where I should put the code he has and how to call it? I don't need the "press ALT+F11 to open the VBA editor," tutorial, but I do need the "here is where you should put your code" and the "here is how you will call your code" tutorials. Any advice?

  4. #4
    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.

  5. #5
    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

  6. #6
    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
    no problems First set up the files and folder as per jb example and run the code
    it will give an idea of what will happen

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

    Re: Combining data from multiple Excel Files

    Thanks Pike,

    The problems begin early. I used Sample2.xlsm and created a new module that I added JB's code to. It unfortunately quickly fails and gives me a "Run-time error '9': Subscript out of range" error. When I debug, the following line of code is highlighted.

     Sheets("Master").Activate   'sheet report is built into
    Any ideas?

    Thanks,
    Rob

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

    Re: Combining data from multiple Excel Files

    OK. I got that part to work. Now I have all of the data in one very large spreadsheet. What is the best way to mine through it to gather the data that I need now?

    Thanks,
    Rob
    Last edited by robbyvegas; 10-05-2010 at 06:30 AM.

  9. #9
    Registered User
    Join Date
    07-25-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Combining data from multiple Excel Files

    Hi robbyvegas,

    I know it was a few years ago but do you remember how you overcame the problem you had here?
    I am trying to do something almost identical to what you were doing but am having a little bit of trouble getting going!

    Thanks
    Alex

  10. #10
    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

    good work . i was a little unsure from your sample.xls which cell{s} to save
    this bit well change to find the cells to copy
     '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
    what we need to do is look in certain cell if they have a values then copy the ranges

    i think its a5 with row 7 to 10 and a15 with rows 17 to 20? or can you highlight the stuff you need.

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

    Re: Combining data from multiple Excel Files

    OK. Working on it. Out of curiosity. What is the best VBA book out there?

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

    Re: Combining data from multiple Excel Files

    OK, I'm working on this task again and I have a few thoughts that might simplify the tasks if you can help me figure out how to implement them.

    First, I believe that I will always need B3, G24, and J24 of each spreadsheet. Can you help me to amend the code
      '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
    to pick up these two cells from each spreadsheet?

    Second, I think that there is a command to have excel create separate worksheets where you have white space between sets of data. Is that correct? If so, can you remind me of what function will do this (if I'm thinking of a macro that you are familiar with and not a function, please let me know)?

    Thank you,

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

    Re: Combining data from multiple Excel Files

    Third, using this great code to pull all of these separate files together, is there then a way to automatically transpose all of the rows to columns, and putting all of the data into colums A & B?

    I think that these three things would go a long way towards simplifying my life tonight.

    Thank you again for all of the help on this.

  14. #14
    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

    I have now have five;
    john walkenback - VBA for dummies and power programming
    julitta Korol - VBA programming
    Paul Lomax VBscript
    and getz/gilbert VBA developers hand book

    there are plenty around but for staters you can go by John Walkenback
    the best place is the library

  15. #15
    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

    Hey robbyvegas

    most of the code can go since you only need three values form each workbook

    try this..
    'This is the section to customize, replace with your own action code as needed
            'Find last row and copy data
              
              Range("B3").Copy wbkNew.Sheets("Master").Range("A" & NR)
              Range("G24").Copy wbkNew.Sheets("Master").Range("B" & NR)
              Range("J24").Copy wbkNew.Sheets("Master").Range("C" & NR)
             
             
             '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
    not sure about the "white space " but we can make new sheets

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

    Re: Combining data from multiple Excel Files

    Thanks,

    Let me clarify. I always need those three cells AND the data in rows 6-10 AND 16-20, from column L to wherever there is an end of data in any cell that column. This last part is the part that I would like to put in a sheet that is separate from those other 3 values we have been discussing, and auto-transpose the rows, putting them all one after the other in columns 1 & 2.

    I can't tell you how grateful I am for your help and guidance on this.

    Thank you,

    Rob
    Last edited by robbyvegas; 10-07-2010 at 01:57 AM.

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

    Re: Combining data from multiple Excel Files

    I could really use some help ASAP if anyone knows how to do this...

    I'm trying to use the code that Pike posted for me above as follows:

     '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)
         MsgBox "Please select a folder with files to consolidate"
        Do
            With Application.FileDialog(msoFileDialogFolderPicker)
                .AllowMultiSelect = False
                .Show
                If .SelectedItems.Count > 0 Then
                    fPath = .SelectedItems(1) & "\"
                    Exit Do
                Else
                    If MsgBox("No folder chose, do you wish to abort?", _
                        vbYesNo) = vbYes Then Exit Sub
                End If
            End With
        Loop
        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
            
              Range("B3").Copy wbkNew.Sheets("Master").Range("A" & NR)
              Range("G24").Copy wbkNew.Sheets("Master").Range("B" & NR)
              Range("J24").Copy wbkNew.Sheets("Master").Range("C" & NR)
             
             
             '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
    I believe that I'm specifically having trouble with the following bit:

      'This is the section to customize, replace with your own action code as needed
            'Find last row and copy data
            
              Range("B3").Copy wbkNew.Sheets("Master").Range("A" & NR)
              Range("G24").Copy wbkNew.Sheets("Master").Range("B" & NR)
              Range("J24").Copy wbkNew.Sheets("Master").Range("C" & NR)
    It runs fine, but the returned values are mostly #REF because they refer to items that are calculated from other values in the source worksheets. Is there a way to do the equivalent of a paste special to paste over just the values of the data that I'm copying?

    Thanks,
    Rob

  18. #18
    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

    ok well get this part right
    Range("B3").value.Copy wbkNew.Sheets("Master").Range("A" & NR)
              Range("G24").value wbkNew.Sheets("Master").Range("B" & NR)
              Range("J24").value wbkNew.Sheets("Master").Range("C" & NR)
    the next bit we will add some if so that when the cells have values they get copied

  19. #19
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Combining data from multiple Excel Files

    Actually, I think it would be:
    wbkNew.Sheets("Master").Range("A" & NR).Value = Range("B3").Value
    wbkNew.Sheets("Master").Range("B" & NR).Value = Range("G24").Value 
    wbkNew.Sheets("Master").Range("C" & NR).Value = Range("J24").Value
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

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

    Re: Combining data from multiple Excel Files

    Ah! Thank you! That works.

    Now I need to figure out how to copy L6:L10 - DD6:DD10 and to transpose their values and paste it into a new worksheet (Master2?). Can you guys help?

    Thank you both!

    Rob

  21. #21
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Combining data from multiple Excel Files

    You'd need to more specific with your references... from exactly where to exactly where (and not a mixed reference... one long specific destination address).

    And what about the next time through the loop? What would the exact source/destination references be then?

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

    Re: Combining data from multiple Excel Files

    I apologize. Please see the attached files.

    In Sample.xls, I'd like to copy L6:L10 - DD6:DD10, and L16:L20 - DD16:DD20. Then as shown in the Count worksheet of Sample2.xlsx I'd like to transpose the matrices of copied data, and to paste the values of these cells into the columns 2-5 of a Master2 worksheet, consecutively for each excel file in the folder.

    Thanks again. I really appreciate the help. Please let me know if any of what I'm saying doesn't make sense.
    Attached Files Attached Files

  23. #23
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Combining data from multiple Excel Files

    To copy the range L6:L10 over into DD6:DD10 on the same sheet and then to copy L16:L20 over into DD16:DD20 on the same sheet is simply:
    Range("L6:L10").Copy  Range("DD6")
    Range("L16:L10").Copy  Range("DD16")

    I don't see the correlation between the sample data (sample.xlsx) and the sample results (sample2.xlsx) so I'll let you figure that out and give you a simple example of code that copies a vertical range and transposes it as it pastes.
    Range("L6:L10").Copy
    wbkNew.Sheets("Master").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteAll, Transpose:=True

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

    Re: Combining data from multiple Excel Files

    Thank you. I suppose you don't see a correlation because I don't know how to check to see if there is an empty column. DD is the furthest that any of my workseets goes to. Is there a more graceful way to approach this?

    Thanks again.

  25. #25
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Combining data from multiple Excel Files

    Why are you copying the data over to column DD? As shown in the second example you can copy and transpose directly into the destination sheet, but it takes two lines of code per copy/paste.

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

    Re: Combining data from multiple Excel Files

    I'm currently copying to column DD because some of the worksheets that I need to process have data all the way out to that column. Is there a way to automatically detect when no row of the selection contains a value? That would be the more elegant way to approach this I suppose.

  27. #27
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Combining data from multiple Excel Files

    Again I ask: "What is the purpose of copying data from column L to column DD (or EE or whatever). What's the data for once it gets to column DD?"

  28. #28
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Excel 2007 : Combining data from multiple Excel Files

    Allen123,

    Welcome to the Forum, unfortunately:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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