Results 1 to 10 of 10

Combine multiple workbooks into one single workbook

Threaded View

maacmaac Combine multiple workbooks... 02-22-2010, 12:43 AM
JBeaucaire Re: Combine multiple... 02-22-2010, 03:29 AM
maacmaac Re: Combine multiple... 02-23-2010, 12:22 AM
umesh_uvg Re: Combine multiple... 02-23-2010, 06:49 PM
jabryantiii Re: Combine multiple... 02-23-2010, 07:51 PM
JBeaucaire Re: Combine multiple... 02-23-2010, 08:16 PM
maacmaac Re: Combine multiple... 02-23-2010, 09:03 PM
JBeaucaire Re: Combine multiple... 02-23-2010, 09:19 PM
maacmaac Re: Combine multiple... 02-23-2010, 09:29 PM
JBeaucaire Re: Combine multiple... 02-23-2010, 09:38 PM
  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Combine multiple workbooks into one single workbook

    I am trying to combine ~300 workbooks into one single workbook. All 300 workbooks have the exact same header. I tried using the code from thread https://www.excelforum.com/showthread.php?p=696435 but nothing is being copied over. The only difference between my example and the other is I only need to take data from the first sheet in each data workbook. All the workbooks are located in following directory
     F:\Excel Tips\Combine Workbooks\WorkbookData
    The “master file” is located in another directory. The “master file” also has the same header as the data workbooks. Basically, I want to retrieve all data (excluding the header) from the first data workbook and copy to the master file. Then I want to go to the second workbook and retrieve all data from the second data workbook and copy to master file, and so on. The code I am using to combine is as follows:
     Sub Get_Value_From_All()
        Dim wbSource As Workbook
        Dim wbThis As Workbook
        Dim rToCopy As Range
        Dim uRng   As Range
        Dim rNextCl As Range
        Dim lCount As Long
        Dim bHeaders As Boolean
        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow   As Long
        Dim CalcMode As Long
        Dim ViewMode As Long
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
    
            On Error Resume Next
    
            Set wbThis = ThisWorkbook
            'clear the range except  headers
            Set uRng = wbThis.Worksheets(1).UsedRange
            If uRng.Cells.Count <= 1 Then
                'no data in master sheet
                bHeaders = False
                GoTo search
            End If
            uRng.Offset(1, 0).Resize(uRng.Rows.Count - 1, _
                                     uRng.Columns.Count).Clear
    search:
            With .FileSearch
                .NewSearch
                'Change path to suit
                .LookIn = "F:\Excel Tips\Combine Workbooks\WorkbookData"
                .FileType = msoFileTypeExcelWorkbooks
    
                If .Execute > 0 Then    'Workbooks in folder
                    For lCount = 1 To .FoundFiles.Count    ' Loop through all.
                        'Open Workbook x and Set a Workbook  variable to it
                        Set wbSource = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                        For i = 1 To Sheets.Count - 1
                            Set rToCopy = wbSource.Worksheets(i).UsedRange
                            Set rNextCl = wbThis.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
                            If bHeaders Then
                                'headers exist so don't copy
                                rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, _
                                                            rToCopy.Columns.Count).Copy rNextCl
                                'no headers so copy
                                'place headers in Row 2
                            Else: rToCopy.Copy Cells(1, 1)
                                bHeaders = True
                            End If
                        Next i
                        wbSource.Close False     'close source workbook
                    Next lCount
                Else: MsgBox "No workbooks found"
                End If
            End With
    
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
        End With
    'not checked following code
                With ActiveSheet
                    .Select
        
                    ViewMode = ActiveWindow.View
                    ActiveWindow.View = xlNormalView
        
                    .DisplayPageBreaks = False
        
                    Firstrow = .UsedRange.Cells(1).Row
                    Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
                    For Lrow = Lastrow To Firstrow Step -1
                        With .Cells(Lrow, "A")
                            If Not IsError(.Value) Then
                                If .Value = "" Then .EntireRow.Delete
                            End If
                        End With
                    Next Lrow
                End With
                On Error GoTo 0
                ScreenUpdating = True
                DisplayAlerts = True
                EnableEvents = True
            'End With
    End Sub
    Thank you in advance for any assistance.
    Last edited by maacmaac; 02-23-2010 at 09:29 PM.

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