Solved

Copy all worksheets in a file to an open workbook

Posted on 2011-09-29
2
199 Views
Last Modified: 2013-06-07
Hi,

I have a workbook open. I want to copy all worksheets from all workbooks (in a folder) to a workbook I am creating (using macro)

I have tried searching here and internet but I feel my problem is unique. This is due to the fact that I am already using a chunk of coding to get all the file in a specific folder and merge them together in a single worksheet (of an opened workbook)

Among this big chunk of coding, I have the following function

1. Function to count the # of files in a folder

2. Function to get file names
3. Function where the files are merged

In which function do you think its the best to do what I require? Or should I write another function that uses #1 & #2 and then copy paste their contents (worksheets) into another opened file?

Thanks for your input!

-
Shanan
0
Comment
Question by:Shanan212
2 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 36817061
Try this.
Sub GetThemAll()
Dim wbDst As Workbook
Dim wbSrc As Workbook
Dim wsDst As Worksheet
Dim strPath As String
Dim strFileName As String

    strPath = "C:\test\"    ' change path as required

    Set wbDst = ThisWorkbook


    strFileName = Dir(strPath & "*.xls")

    Application.ScreenUpdating = False

    While strFileName <> ""

        Set wsDst = wbDst.Worksheets(wbDst.Worksheets.Count)

        Set wbSrc = Workbooks.Open(strPath & strFileName)

        wbSrc.Worksheets.Copy After:=wsDst

        wbSrc.Close False

        strFileName = Dir
    Wend

    Application.ScreenUpdating = True

End Sub         

Open in new window

Note, this has absolutely no error checking.
0
 
LVL 13

Author Closing Comment

by:Shanan212
ID: 36817476
Thank you!

Error checking is not needed since my original functions that perform other things have error checking and that would take care of 'if file exists, if file has tabs, if tabs has data' stuff

Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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 demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

820 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