Solved

Tricky copy and paste macro from multiple workbooks

Posted on 2013-01-09
7
220 Views
Last Modified: 2013-01-15
Hi Experts

Need a macro that opens all the workbooks in the c: drive file paths c:\documents and setting\test

One by one and copies the data in worksheet "cm" and and pastes the data onto a master workbook same file path called "Master Template" and into worksheet "cm1". So the data is stacked one under Neath the other...

Workbooks in c: drive file paths c:\documents and setting\test are:-

1. Customer.xls
2. Supplier.xls
3. Temporary.xls
4. Resource.xls
0
Comment
Question by:route217
  • 4
  • 3
7 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 38759982
Try this which assumes the Master Template worksheet needs to be opened.
Sub Conso()
Dim wbDst As Workbook
Dim wbSrc As Workbook
Dim strFilename As String

    Set wbDst = Workbooks.Open("C:\Documents and Settings\Test\Master Template.xls")
    
    strFilename = Dir("C:\Documents and Settings\Test\*.xls")
    
    While strFilename <> ""
    
        If strFilename <> "Master Template.xls" Then
        
            Set wbSrc = Workbooks.Open("C:\Documents and Settings\Test\" & strFilename)
            
                 wbSrc.Worksheets("cm").UsedRange.Copy wbDst.Worksheets("cm1").Range("A" & Rows.Count).End(xlUp).Offset(1)

            
            wbSrc.Close
        End If
        
        strFilename = Dir()
        
    Wend
                 
End Sub

Open in new window

0
 

Author Comment

by:route217
ID: 38760235
Immoire

Thanks for the feedback... Are saying run the macro from the master file?
0
 

Author Comment

by:route217
ID: 38760252
Ps last question

Immoire this macro will open up all the workbook in the specified c: drive one by one and paste the data into the master file one after the other...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 33

Expert Comment

by:Norie
ID: 38760289
It will open the master workbook, then one by one open the rest of the xls files in the folder, copy the data from sheet 'cm' to sheet 'cm1' in the master workbook.

If you want to run it from the master workbook it can easily be changed.
0
 

Author Comment

by:route217
ID: 38760403
That would be great if it could be changed to run from master file ... And is the data pasted into master file one after another I.e each file in turn??
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 38760637
That's how the data is pasted.

Here's the code so it will run from the master file.
Sub Conso()
Dim wbDst As Workbook
Dim wbSrc As Workbook
Dim strFilename As String

    Set wbDst = ThisWorkbook  ' Workbooks.Open("C:\Documents and Settings\Test\Master Template.xls")
    
    strFilename = Dir("C:\Documents and Settings\Test\*.xls")
    
    While strFilename <> ""
    
        If strFilename <> wbDst.Name Then
        
            Set wbSrc = Workbooks.Open("C:\Documents and Settings\Test\" & strFilename)
            
                 wbSrc.Worksheets("cm").UsedRange.Copy wbDst.Worksheets("cm1").Range("A" & Rows.Count).End(xlUp).Offset(1)

            
            wbSrc.Close
        End If
        
        strFilename = Dir()
        
    Wend
                 
End Sub

Open in new window

0
 

Author Comment

by:route217
ID: 38779858
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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 create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

679 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