Solved

Copy all worksheets in a file to an open workbook

Posted on 2011-09-29
2
197 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

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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
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

20 Experts available now in Live!

Get 1:1 Help Now