+ Reply to Thread
Results 1 to 3 of 3

Combine same sheet from multiple workbooks in the same folder

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-06-2012
    Posts
    139

    Combine same sheet from multiple workbooks in the same folder

    Hi,

    I have same format workbooks in a folder.

    I would like to combine one specific sheet into one workbook. But i have to do this for multiple folders, so is it possible to do this with an vba?

    I have a code that combines all OPEN workbooks, but i would like to combine all workbook in this path without opening it.

    Sub GetSheets()
    Path = "C:\Users\dt\Desktop\dt kte\"
    Filename = Dir(Path & "*.xls")
      Do While Filename <> ""
      Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
         For Each Sheet In ActiveWorkbook.Sheets
         Sheet.Copy After:=ThisWorkbook.Sheets(1)
      Next Sheet
         Workbooks(Filename).Close
         Filename = Dir()
      Loop
    End Sub

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2504
    Posts
    13,620

    Re: Combine same sheet from multiple workbooks in the same folder

    One of your fellow countrymen has done the job for all of us http://www.rondebruin.nl/win/addins/rdbmerge.htm

  3. #3
    Forum Contributor
    Join Date
    01-18-2009
    Location
    Montreal
    MS-Off Ver
    MS Office 2016
    Posts
    111

    Re: Combine same sheet from multiple workbooks in the same folder

    Give this a try:
    Dim dic             As Object
    Dim Counter         As Long
    Sub ConsolidateWorkbooks()
        
        Dim r           As Long
        Dim c           As Long
        Dim n           As Long
        Dim j           As Long
        Dim Fldr        As String
        Dim Fname       As String
        Dim wbkActive   As Workbook
        Dim wbkSource   As Workbook
        Dim Dest        As Range
        Dim d, k()
        
        '// User settings
        Const SourceFileType        As String = "xls*"  'xls,xlsx,xlsb,xlsm
        Const DestinationSheet      As String = "Sheet1"
        Const DestStartCell         As String = "A1"
        Const MaxRows               As Long = 50000
        Const MaxCols               As Long = 100
        Const StartRow              As Long = 2
        '// End
        
        Application.ScreenUpdating = False
        Counter = 0
        With Application.FileDialog(4)
            .Title = "Select source file folder"
            .AllowMultiSelect = False
            If .Show = -1 Then
                Fldr = .SelectedItems(1)
            Else
                GoTo Xit
            End If
        End With
        
        
        Set dic = CreateObject("scripting.dictionary")
            dic.comparemode = 1
        
        Set wbkActive = ThisWorkbook
        
        ReDim k(1 To MaxRows, 1 To MaxCols)
        
        Set Dest = wbkActive.Worksheets(DestinationSheet).Range(DestStartCell)
        
        Fname = Dir(Fldr & "\*." & SourceFileType)
        
        Do While Len(Fname)
            If wbkActive.Name <> Fname Then
                Set wbkSource = Workbooks.Open(Fldr & "\" & Fname)
                With wbkSource.Worksheets(1)
                    d = .Range("a1").CurrentRegion
                    UniqueHeaders Application.Index(d, 1, 0)
                    For r = StartRow To UBound(d, 1)
                        If Len(d(r, 1)) Then
                            n = n + 1
                            For c = 1 To UBound(d, 2)
                                If Len(Trim$(d(1, c))) Then
                                    j = dic.Item(Trim$(d(1, c)))
                                    k(n, j) = d(r, c)
                                End If
                            Next
                        End If
                    Next
                    Erase d
                End With
                wbkSource.Close 0
                Set wbkSource = Nothing
            End If
            Fname = Dir()
        Loop
        
        If n Then
            Dest.Resize(, dic.Count) = dic.keys
            Dest.Offset(1).Resize(n, dic.Count) = k
            MsgBox "Done! Importing Content"
        End If
    Xit:
        Application.ScreenUpdating = True
        
    End Sub
    Private Sub UniqueHeaders(ByRef DataHeader)
        
        Dim i   As Long
        Dim j   As Long
        
        With Application
            j = .ScreenUpdating
            .ScreenUpdating = False
        End With
        
        For i = LBound(DataHeader) To UBound(DataHeader)
            If Len(Trim$(DataHeader(i))) Then
                If Not dic.exists(Trim$(DataHeader(i))) Then
                    Counter = Counter + 1
                    dic.Add Trim$(DataHeader(i)), Counter
                End If
            End If
        Next
        
        Application.ScreenUpdating = j
        
    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. [SOLVED] Copy Same Sheet to Multiple Workbooks in Folder
    By allie14 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-02-2014, 10:13 AM
  2. [SOLVED] combine 1st sheet from all workbooks in folder
    By onbeillp111 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-18-2014, 05:14 PM
  3. Combine Multiple Workbooks into one box spread sheet
    By Oszie in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-02-2013, 07:02 AM
  4. Combine multiple workbooks in a single sheet
    By thomas_people in forum Excel General
    Replies: 2
    Last Post: 09-13-2012, 03:35 PM
  5. combine workbooks stored in the same folder into 1 a summary sheet.
    By lrm75uk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-22-2010, 04:09 PM

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