Solved

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

Posted on 2014-07-22
5
413 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 69

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 69

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 27

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

749 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