Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-23
10
Medium Priority
?
794 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
[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
  • 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
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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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 article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
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 in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

670 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