+ Reply to Thread
Results 1 to 9 of 9

Pulling Data from Multiple workbooks to one consolidated sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    10-30-2018
    Location
    Sydney Australia
    MS-Off Ver
    Office 365
    Posts
    4

    Pulling Data from Multiple workbooks to one consolidated sheet

    So I'm trying to pull the data from a specific sheet in a list of work books into one master sheet. The data is in the sheet "Data", and per workbook there is one row and 12 columns. the current formula I have is(I've included the error next to it related line):
    Sub getDataFromWbs()

    Dim wb As Workbook, ws As Worksheet
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    'This is where you put YOUR folder name
    Set fldr = fso.GetFolder("C:\Users\<My name>\Desktop\Temp\")
    
    'Next available Row on Master Workbook
    y = ThisWorkbook.Sheets("Data").Cells(Rows.Count, 1).End(xlUp).Row + 1 <-------- For this line i keep getting runtime error '9', subscript out of range
    
    'Loop through each file in that folder
    For Each wbFile In fldr.Files
        
        'Make sure looping only through files ending in .xlsx (Excel files)
        If fso.GetExtensionName(wbFile.Name) = "xls" Then
          
          'Open current book
          Set wb = Workbooks.Open(wbFile.Path)
          
          'Loop through each sheet (ws)
          For Each ws In wb.Sheets
              'Last row in that sheet (ws)
              wsLR = ws.Cells(Rows.Count, 1).End(xlUp).Row
              
              'Loop through each record (row 2 through last row)
              For x = 2 To wsLR
                'Put column 1,2,3 and 4 of current sheet (ws) into row y of master sheet, then increase row y to next row
                ThisWorkbook.Sheets("Data").Cells(y, 1) = ws.Cells(x, 1) 'col 1
                ThisWorkbook.Sheets("Data").Cells(y, 2) = ws.Cells(x, 2) 'col 1
                ThisWorkbook.Sheets("Data").Cells(y, 3) = ws.Cells(x, 3) 'col 1
                ThisWorkbook.Sheets("Data").Cells(y, 4) = ws.Cells(x, 4) 'col 1
                y = y + 1
              Next x
              
              
          Next ws
          
          'Close current book
          wb.Close
        End If
    
    Next wbFile
    
    End Sub
    Last edited by Stokess; 10-30-2018 at 07:27 PM.

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Pulling Data from Multiple workbooks to one consolidated sheet

    Hi,

    Can you try this instead :
    y = ThisWorkbook.Sheets("Data").Cells(ThisWorkbook.Sheets("Data").Rows.Count, 1).End(xlUp).Row + 1 '<-------- For this line i keep getting runtime error '9', subscript out of range
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Registered User
    Join Date
    10-30-2018
    Location
    Sydney Australia
    MS-Off Ver
    Office 365
    Posts
    4

    Re: Pulling Data from Multiple workbooks to one consolidated sheet

    Thanks, but now I'm having the issue of it pulling data from all the sheets within the workbook. Is it possible to make it pull from only one sheet(all with the name data) within each workbook?

  4. #4
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Pulling Data from Multiple workbooks to one consolidated sheet

    Yes it's possible. But can you clarify which data you want to pull from those workbook and copy to the master workbook?
    Is it only the last row?
    all rows?
    12 columns?

    Thanks,

    (Having a sample workbook would help...)

  5. #5
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Pulling Data from Multiple workbooks to one consolidated sheet

    Like this?

    Sub getDataFromWbs()
       Dim wbM As Workbook
       Dim wb As Workbook, ws As Worksheet
       Dim fldr, wbFile
    
       'Set the Master workbook
       Set wbM = ThisWorkbook
    
       Set fso = CreateObject("Scripting.FileSystemObject")
    
       'This is where you put YOUR folder name
       Set fldr = fso.GetFolder("C:\Users\<My name>\Desktop\Temp\")
    
       'Loop through each file in that folder
       For Each wbFile In fldr.Files
        
          'Make sure looping only through files ending in .xlsx (Excel files)
          If fso.GetExtensionName(wbFile.Name) = "xls" Then
          
             'Open current book
             Set wb = Workbooks.Open(wbFile.Path)
          
             'Look only in sheets DATA
             With wb.Sheets("Data")
                    
                'Assuming that you want copy the last row / 12 columns
                'to the next row in sheets "DATA" of the Master workbook:
                .Range("A66666").End(xlUp).Resize(1, 12).Copy wbM.Sheets("Data").Range("A66666").End(xlUp).Offset(1, 0)
             
             End With
          
             'Close current book
             wb.Close
          End If
    
       Next wbFile
    
    End Sub

  6. #6
    Registered User
    Join Date
    10-30-2018
    Location
    Sydney Australia
    MS-Off Ver
    Office 365
    Posts
    4

    Re: Pulling Data from Multiple workbooks to one consolidated sheet

    Sent you a private message with the data sets

  7. #7
    Registered User
    Join Date
    10-30-2018
    Location
    Sydney Australia
    MS-Off Ver
    Office 365
    Posts
    4

    Re: Pulling Data from Multiple workbooks to one consolidated sheet

    Quote Originally Posted by GC Excel View Post
    Like this?

    Sub getDataFromWbs()
       Dim wbM As Workbook
       Dim wb As Workbook, ws As Worksheet
       Dim fldr, wbFile
    
       'Set the Master workbook
       Set wbM = ThisWorkbook
    
       Set fso = CreateObject("Scripting.FileSystemObject")
    
       'This is where you put YOUR folder name
       Set fldr = fso.GetFolder("C:\Users\<My name>\Desktop\Temp\")
    
       'Loop through each file in that folder
       For Each wbFile In fldr.Files
        
          'Make sure looping only through files ending in .xlsx (Excel files)
          If fso.GetExtensionName(wbFile.Name) = "xls" Then
          
             'Open current book
             Set wb = Workbooks.Open(wbFile.Path) <---------- Im now getting that error code 9, subcript out of range here
          
             'Look only in sheets DATA
             With wb.Sheets("Data")
                    
                'Assuming that you want copy the last row / 12 columns
                'to the next row in sheets "DATA" of the Master workbook:
                .Range("A66666").End(xlUp).Resize(1, 12).Copy wbM.Sheets("Data").Range("A66666").End(xlUp).Offset(1, 0)
             
             End With
          
             'Close current book
             wb.Close
          End If
    
       Next wbFile
    
    End Sub
    above i have included another spot where i am now getting error code

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Pulling Data from Multiple workbooks to one consolidated sheet

    Different method
    Sub test()
        Dim myDir As String, fn As String, x, y, i As Long
        Application.ScreenUpdating = False
        myDir = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\temp\"
        fn = Dir(myDir & "*.xls")
        Do While fn <> ""
            x = ExecuteExcel4Macro("match(rept(""z"",10),'" & myDir & "[" & fn & "]data'!c1,1)")
            If IsError(x) Then
                x = ExecuteExcel4Macro("match(10^10,'" & myDir & "[" & fn & "]data'!c1,1)")
            Else
                y = ExecuteExcel4Macro("match(10^10,'" & myDir & "[" & fn & "]data'!c1,1)")
                If Not IsError(y) Then x = Application.Max(x, y)
            End If
            If Not IsError(x) Then
                With Sheets("data").Range("a" & Rows.Count).End(xlUp)(2).Resize(, 12)
                    .Formula = "='" & myDir & "[" & fn & "]data'!a" & x
                    .Value = .Value
                End With
            End If
            fn = Dir
        Loop
        Application.ScreenUpdating = True
    End Sub

  9. #9
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Pulling Data from Multiple workbooks to one consolidated sheet

    Hi,

    Here's a revised code.
    it is not clear which columns you want to copy to your master workbook.
    As an example, I put columns 11-12-13-14. Change as required.

    Sub getDataFromWbs2()
       Dim wbM As Workbook
       Dim wb As Workbook, ws As Worksheet
       Dim fldr, wbFile, fso
       Dim Lastrow As Long
       
       Application.ScreenUpdating = False
    
       'Set the Master workbook
       Set wbM = ThisWorkbook
    
       Set fso = CreateObject("Scripting.FileSystemObject")
    
       'This is where you put YOUR folder name
       'Set fldr = fso.GetFolder("C:\Users\<My name>\Desktop\Temp\")
       Set fldr = fso.GetFolder("C:\Temp\")
    
       'Loop through each file in that folder
       For Each wbFile In fldr.Files
        
          'Make sure looping only through files ending in .xlsx (Excel files)
          If fso.GetExtensionName(wbFile.Name) = "xls" Then
          
             'Open current book
             Set wb = Workbooks.Open(wbFile.Path)
          
             'Look only in sheets DATA
             With wb.Sheets("Data")
             
                'last row master workbook
                Lastrow = wbM.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1
                
                'copy to master workbook columns 11-12-13-14  (adapt as required)
                wbM.Sheets(1).Cells(Lastrow, 1) = .Cells(2, 11)   'Name
                wbM.Sheets(1).Cells(Lastrow, 2) = .Cells(2, 12)   'Title
                wbM.Sheets(1).Cells(Lastrow, 3) = .Cells(2, 13)   'Date
                wbM.Sheets(1).Cells(Lastrow, 4) = .Cells(2, 14)   'Sales Amount
                
             End With
          
             'Close current book
             wb.Close
             
          End If
          
       Next wbFile
       
       Application.ScreenUpdating = True
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Multiple workbooks consolidated into single master sheet
    By TRazzo in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-31-2018, 09:16 AM
  2. [SOLVED] VBA to extract data from multiple workbooks into one consolidated document
    By The_Snook in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-19-2018, 04:39 AM
  3. Pulling data from multiple workbooks into one
    By prosemur in forum Excel General
    Replies: 0
    Last Post: 03-25-2014, 10:30 AM
  4. Overall sheet pulling in info from multiple workbooks
    By Seancsn in forum Excel General
    Replies: 4
    Last Post: 04-10-2013, 10:02 AM
  5. Pulling Up Data From Multiple Workbooks
    By gedaliahr in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-24-2013, 01:07 PM
  6. Copy data from multiple workbooks into consolidated/master workbook
    By mobro1234 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-02-2012, 02:42 PM
  7. Pulling Up Data From Multiple Workbooks
    By DomV in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 12-10-2009, 03:20 AM

Tags for this Thread

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