Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBA to copy multiple Excel 2007 worksheets from one workbook to another

Posted on 2011-02-23
10
Medium Priority
?
796 Views
Last Modified: 2012-05-11
I need to copy a fixed number (say 25) of worksheets from an Excel file to another Excel file.
The first XLS contains raw data and the second is a template that formats the data and creates charts.  The worksheets have different names in each XLS file.  I don't need to copy the entire worksheet, just a range such as A1:E10.
 I know I can hard code the worksheet names and do this, but I'm hoping there's a way to just loop through and get the first 25 worksheets from the first file and add the data to the first 25 worksheets in the second file.
0
Comment
Question by:Ed_CLP
  • 5
  • 3
  • 2
10 Comments
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 1000 total points
ID: 34960670
Something like this should work:


Sub MakeItHappen()

    Dim SourceWb As Workbook, DestWb As Workbook
    Dim Counter As Long

    Set SourceWb = Workbooks("Source.xls")
    Set DestWb = Workbooks("Destination.xls")

    For Counter = 1 To 25
        SourceWb.Worksheets(Counter).[a1:e10].Copy DestWb.Worksheets(Counter).[a1]
    Next

    MsgBox "Done"

End Sub
0
 

Author Comment

by:Ed_CLP
ID: 34960896
Thanks for the fast reply.  I'm getting a Run-time error 9, Subscript out of range on
Set DestWb = Workbooks("destination.xls")
Any idea why?  

0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 34960951
You need to update the names of the 'source' and 'destination' workbooks :)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34960980
Ed_CLP: Also one quick question. When you say fixed number of worksheets, what do you exactly mean? Because if you have 30 worksheets then the above code will copy only the first 25 and not the 25 sheets that you actually want to copy...

Sid
0
 

Author Comment

by:Ed_CLP
ID: 34961352
@matthewspatrick
I actually named my files source and destination;
Set SourceWb = Workbooks("source.xlsm")
    Set DestWb = Workbooks("destination.xlsm")
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34961406
Ed_CLP: Seems like you ignored my question...

Anyways here is another question based on your above post.

Is your destination workbook closed or open?

Sid
0
 

Author Comment

by:Ed_CLP
ID: 34961524
@SiddharthRout - I tried it both with the destination.xlsm open and closed and get the same error.
On the number of worksheets, I want a fixed number not all of them.
0
 
LVL 30

Assisted Solution

by:SiddharthRout
SiddharthRout earned 1000 total points
ID: 34961603
If the workbook is closed then you will have to refer to it as

Set DestWb = Workbooks.Open("C:\destination.xls")

Regarding "fixed number" do you want the first 25 sheet or 25 sheets that you want to select?

Sid
0
 

Author Comment

by:Ed_CLP
ID: 34961782
Setting the file path and using the closed workbook is working.
I split the points because you were both very helpful in answering my question.
Thanks!
0
 

Author Closing Comment

by:Ed_CLP
ID: 34961798
Great answers!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

782 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