Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
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…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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