combining a ton of workbooks

getret

New Member
Joined
May 9, 2007
Messages
2
i've got a folder full of workbooks.
each workbook has anywhere from 3-12 worksheets.
the names of the worksheets are the names of employees.
when the master-workbook is created, the names of the sheets need to be something like
empA.1, empA.2, EmpA.3, .... EmpA.n, EmptB.1 EmpB.2, EmpB.3, ... EmpB.n

is this even possible? i'm not very good with macros, and i've not found any that combine multiple sheet workbooks into one workbook.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
This assumes that each of the sheets has a unique name.
Create your new Master book. Put this code in a module for that workbook.
When you run this code, each sheet in all of the open workbooks will be checked to see if a duplicate sheet exists in the Master Book. If there is no duplicate sheet, one will be created.
You can run this code repeatedly (you don't have to have all of your source workbooks open at the same time.)
By "duplicate sheet" I mean a sheet with the same name. When this code runs it will copy the old sheet to a new sheet. But if the sheet's name is already in MasterBook, it will do nothing, it will not update masterBook sheetAlpha to match sourceBook sheetAlpha.
Code:
Sub addToMaster()
Dim xBook As Workbook
Dim xSheet As Worksheet
Dim xStr As String
For Each xBook In ThisWorkbook.Parent.Workbooks
    If Not (ThisWorkbook Is xBook) Then
        For Each xSheet In xBook.Sheets
            On Error Resume Next
            xStr = ThisWorkbook.Sheets(xSheet.Name).Name
            If Err <> 0 Then
                With xSheet
                   ThisWorkbook.Sheets.Add.Name = .Name
                   .UsedRange.Copy _
                       Destination:=ThisWorkbook.Sheets(.Name).Range(.UsedRange.Address)
                 End With
            End If
            On Error Goto 0
        Next xSheet
    End If
Next xBook
End Sub
 
Upvote 0
Here's some code that could be merged with the previous post. This one will ask you to select the folder containing the workbooks and then put copies of every worksheet from every Excel file in that folder into a new workbook:

Code:
Option Explicit
Public strPath As String

Public Type BROWSEINFO
    hOwner As Long
    pidlRoot As Long
    pszDisplayName As String
    lpszTitle As String
    ulFlags As Long
    lpfn As Long
    lParam As Long
    iImage As Long
End Type

'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
                                     Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long

Declare Function SHBrowseForFolder Lib "shell32.dll" _
                                   Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

Function GetDirectory(Optional Msg) As String
    Dim bInfo As BROWSEINFO
    Dim path As String
    Dim r As Long, x As Long, pos As Integer

    '   Root folder = Desktop
    bInfo.pidlRoot = 0&

    '   Title in the dialog
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Select a folder."
    Else
        bInfo.lpszTitle = Msg
    End If

    '   Type of directory to return
    bInfo.ulFlags = &H1

    '   Display the dialog
    x = SHBrowseForFolder(bInfo)

    '   Parse the result
    path = Space$(512)
    r = SHGetPathFromIDList(ByVal x, ByVal path)
    If r Then
        pos = InStr(path, Chr$(0))
        GetDirectory = Left(path, pos - 1)
    Else
        GetDirectory = ""
    End If
End Function

Sub CombineWorkbooks()
Dim CurFile As String
Dim DestWB As Workbook
Dim ws As Object 'allows for different sheet types
Dim DirLoc As String
'directory name


   On Error GoTo CombineWorkbooks_Error

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    DirLoc = GetDirectory & "\"
    
    Set DestWB = Workbooks.Add
    
    CurFile = Dir(DirLoc & "*.xls")
    If CurFile = vbNullString Then Exit Sub
    
    Do While CurFile <> vbNullString
        Dim OrigWB As Workbook
        
        Set OrigWB = Workbooks.Open(Filename:=DirLoc & CurFile, ReadOnly:=True)
        
        CurFile = Left(Left(CurFile, Len(CurFile) - 4), 29) 'Limits to valid sheet names
        'and removes ".xls"
        
        For Each ws In OrigWB.Sheets
            ws.Copy After:=DestWB.Sheets(DestWB.Sheets.Count)
        Next
        
        OrigWB.Close SaveChanges:=False
        
        CurFile = Dir
    Loop
    
    Application.DisplayAlerts = False
    DestWB.Sheets(1).Delete
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
    Set DestWB = Nothing

   On Error GoTo 0
   Exit Sub

CombineWorkbooks_Error:
Application.DisplayAlerts = True
    Application.EnableEvents = True
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") "
End Sub
 
Upvote 0
Here's some code that could be merged with the previous post. This one will ask you to select the folder containing the workbooks and then put copies of every worksheet from every Excel file in that folder into a new workbook:
if i toss this into the vb editor i get an error:
"only comments may appear after end sub, end function, or end property".

this is highlighted:
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
 
Upvote 0
Make sure that the code (same as I posted before) below is at the begining of all code in the code module

Code:
Option Explicit 
Public strPath As String 

Public Type BROWSEINFO 
    hOwner As Long 
    pidlRoot As Long 
    pszDisplayName As String 
    lpszTitle As String 
    ulFlags As Long 
    lpfn As Long 
    lParam As Long 
    iImage As Long 
End Type 

'32-bit API declarations 
Declare Function SHGetPathFromIDList Lib "shell32.dll" _ 
                                     Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long 

Declare Function SHBrowseForFolder Lib "shell32.dll" _ 
                                   Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,746
Members
449,050
Latest member
excelknuckles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top