Solved

Append multiple workbooks, all one directory, one sheet each, to one worksheet

Posted on 2014-07-22
5
397 Views
Last Modified: 2014-07-22
I have numerous workbooks (.xls) in one directory, each with one worksheet, all with the same structure.
I need to append all the worksheets.
I started manually with excel 2010. It will take ages. I then started importing and appending in Access 2010. Will be much faster, but still too slow.
Can you help with VBA to do the job either using Excel or using Access. The files look like below.
Example of file.
0
Comment
Question by:Fritz Paul
  • 2
  • 2
5 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 40211202
It will always be a slow task, but can be speeded up in Excel VBA:
1. Switch off sheet online updates/calculations for the result sheet («Worksheet».EnableCalulcation = false)
2. Use the «Range».Copy with destination parameter to append each sheet (after opening it) to the result sheet.
3. Make sure to close each source workbook immediately after having copied its data.

I don't think using Jet drivers (ODBC / OleDB / ...) and handling XLS files as database will be faster. Same applies to using Automation (controlling Excel from Access, PowerShell, VBS or whatever).
0
 

Author Comment

by:Fritz Paul
ID: 40211277
Thanks, but sorry, I don't understand what you are saying. Are those tips to use in VBA? I know too little about VBA to manage that myself.

I need VBA to open each workbook in the directory,
copy the whole range with data,
move to the append worksheet and paste at the bottom of the existing data.
Then close the "copied from worbook" and carry out the same process with the next workbook in the directory.

Regards.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40211292
Sorry, I misread your actions taken to use VBA already, so was just giving hints. I now see you did all that manually. I have to leave, so cannot provide code ATM, but maybe someone else might (it is nothing difficult, just some effort). Else I'll write up something in several hours.
0
 
LVL 26

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 40212234
Try this code, it will prompt you for the files to merge (if you want - the folder containing the files can be hard coded so you wont have to select the files), then create a new work book with all the records.
Code based on a Microsoft sample (http://msdn.microsoft.com/en-us/library/gg549168%28v=office.14%29.aspx) should be fairly fast.
Sub MergeSelectedWorkbooks()

    Dim SummarySheet As Worksheet
    Dim FolderPath As String
    Dim SelectedFiles() As Variant
    Dim NRow As Long
    Dim FileName As String
    Dim NFile As Long
    Dim WorkBk As Workbook
    Dim SourceRange As Range
    Dim DestRange As Range
    Dim LastRow As Long

    ' Create a new workbook and set a variable to the first sheet.
    Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)

    ' Open the file dialog box and filter on Excel files, allowing multiple files
    ' to be selected.
    SelectedFiles = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)

    ' NRow keeps track of where to insert new rows in the destination workbook.
    NRow = 1

    ' Loop through the list of returned file names
    For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
        ' Set FileName to be the current workbook file name to open.
        FileName = SelectedFiles(NFile)

        ' Open the current workbook.
        Set WorkBk = Workbooks.Open(FileName)

        ' Get row number of last used row
        LastRow = WorkBk.Worksheets(1).Cells.Find(What:="*", _
                                                  After:=WorkBk.Worksheets(1).Cells.Range("A1"), _
                                                  SearchDirection:=xlPrevious, _
                                                  LookIn:=xlFormulas, _
                                                  SearchOrder:=xlByRows).Row

        ' Set the cell in column N to be the file name.
        SummarySheet.Range("N" & NRow).Value = FileName
        
        ' Create header row
        Set SourceRange = WorkBk.Worksheets(1).Range("A1:M1")
        Set DestRange = SummarySheet.Range("A1:M1")
        DestRange.Value = SourceRange.Value

        ' Set the source range to be B1 through M?.
        ' Modify this range for your workbooks. It can span multiple rows.
        Set SourceRange = WorkBk.Worksheets(1).Range("A2:M" & LastRow)

        ' Set the destination range to start at column A and be the same size as the source range.
        Set DestRange = SummarySheet.Range("A" & NRow)
        Set DestRange = DestRange.Resize(SourceRange.Rows.Count, SourceRange.Columns.Count)

        ' Copy over the values from the source to the destination.
        DestRange.Value = SourceRange.Value

        ' Increase NRow so that we know where to copy data next.
        NRow = NRow + DestRange.Rows.Count

        ' Close the source workbook without saving changes.
        WorkBk.Close savechanges:=False
    Next NFile

    ' Call AutoFit on the destination sheet so that all data is readable.
    SummarySheet.Columns.AutoFit
End Sub

Open in new window

0
 

Author Closing Comment

by:Fritz Paul
ID: 40212685
Thanks! Sharp!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now