Solved

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

Posted on 2014-07-22
5
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 70

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 70

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

690 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