Solved

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

Posted on 2011-02-23
10
745 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 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
Comment Utility
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
Comment Utility
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 92

Expert Comment

by:Patrick Matthews
Comment Utility
You need to update the names of the 'source' and 'destination' workbooks :)
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
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
Comment Utility
@matthewspatrick
I actually named my files source and destination;
Set SourceWb = Workbooks("source.xlsm")
    Set DestWb = Workbooks("destination.xlsm")
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
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
Comment Utility
@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 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Great answers!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

771 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

11 Experts available now in Live!

Get 1:1 Help Now