Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

15 Experts available now in Live!

Get 1:1 Help Now