?
Solved

consolidate multiple excel files to one

Posted on 2014-10-16
10
Medium Priority
?
417 Views
Last Modified: 2014-10-17
I have an issue.  I have a whole bunch of files in a folder with various different names.  The files I want to merge have the word REC at the start of the file
e.g REC_012014.xlsx, REC_022014.xslx..etc.  They all have the same format, and I just want to extract the first sheet with range A1:D50 in all the files and merge them into one excel file.  How do I go about doing this?
0
Comment
Question by:Qazzim Dungey
[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
  • 4
  • 3
  • 2
10 Comments
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 40386328
Here is something to get you started
Sub MergeFiles()
    Dim directory As String, fileName As String, sheet As Worksheet, i As Integer, j As Integer
    Dim sheet As Worksheet
    
    Application.ScreenUpdating = False
    directory = "C:\test\"
    fileName = Dir(directory & "REC*.xl??")
    Do While fileName <> ""
        Workbooks.Open (directory & fileName)
        sheet = Workbooks(fileName).Worksheets(0)
        sheet.Range("A1:D50").Select
        Selection.Copy
        ' do what you need to here
        fileName = Dir()
    Loop
    
End Sub

Open in new window

To use the above create a new Excel spreadsheet and create a macro with the above code - change the path name as required.

You did not say how you wanted to merge the data so you will need to do that yourself but it is easy to work out the code simply do it manually once and record it in a macro then view the code.
0
 
LVL 26

Expert Comment

by:ProfessorJimJam
ID: 40386513
You would easily accomplish this with free Add-In of Ron de Bruin  http://www.rondebruin.nl/win/addins/rdbmerge.htm
0
 

Author Comment

by:Qazzim Dungey
ID: 40386630
JulianH, your are almost there.  What I want to do with it is merge it vertically, so it joins all the files and is the exact same as the other reports, the only difference is this one will just have a larger amount of data.  The issue I am having is I am trying that but how do I keep record of the last row, so I can append without overwriting.  I am fairly new to VBA.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 58

Expert Comment

by:Julian Hansen
ID: 40386738
That's easy you are copying out 50 rows at a time so you have a loop variable that starts at 0.

Each time you go round you paste to the row = loop_variable * 50

Something like this
Sub MergeFiles()
    Dim directory As String, fileName As String, sheet As Worksheet, i As Integer, j As Integer
    Dim sheet As Worksheet
    Dim Row As Integer
    Dim Cell as String

    Application.ScreenUpdating = False
    directory = "C:\test\"
    fileName = Dir(directory & "REC*.xl??")
    Row = 0
    Do While fileName <> ""
        Workbooks.Open (directory & fileName)
        sheet = Workbooks(fileName).Worksheets(0)
        sheet.Range("A1:D50").Select
        Selection.Copy
        Cell = "A" & ((Row * 50) + 1)
        Range(Cell).Select
        ActiveSheet.Paste
        Row = Row + 1

        fileName = Dir()
    Loop
End Sub

Open in new window

0
 

Author Comment

by:Qazzim Dungey
ID: 40386981
It is giving me a subscript out of range when I test it out.  My guess it is referencing something out of the limit.  Can you spot the error?
0
 
LVL 26

Expert Comment

by:ProfessorJimJam
ID: 40386995
Qazzim,  

Why not use the free Add-In of Ron de Bruin which exactly does what you need.
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 40387034
What line is the error on?
0
 

Author Comment

by:Qazzim Dungey
ID: 40387085
sheet = Workbooks(fileName).Worksheets(0)

ProfessorjimJam, I am not using the utility because I need to get this macro for a bigger problem.  This is just one small part to it.
0
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 40387284
Thats because it is an object and was missing the 'Set'

Did some testing - this should mostly do what you want
Sub Macro1()
'
' Macro1 Macro
'

'
    Dim directory As String, fileName As String, sheet As Worksheet, i As Integer, j As Integer, target As Worksheet
    Dim Row As Integer
    Dim Cell As String, sOrigWS As String, sOrigWB As String
    
    directory = "e:\projects\storevue\spar\"
    fileName = Dir(directory & "*.xlsx")

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Row = 0
    Set target = ActiveSheet
    sOrigWS = ActiveSheet.Name
    sOrigWB = ActiveWorkbook.Name
    Do While fileName <> ""
        Workbooks.Open (directory & fileName)
        Set sheet = Workbooks(fileName).Worksheets(1)
        sheet.Range("A1:D50").Select
        Selection.Copy
        Workbooks(sOrigWB).Activate
        Sheets(sOrigWS).Select
        Cell = "A" & ((Row * 50) + 1)
        Range(Cell).Select
        ActiveSheet.Paste
        Workbooks(fileName).Close
        Row = Row + 1

        fileName = Dir()
    Loop
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

762 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