Solved

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

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

Expert Comment

by:Patrick Matthews
ID: 34960951
You need to update the names of the 'source' and 'destination' workbooks :)
0
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

911 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

17 Experts available now in Live!

Get 1:1 Help Now