Solved

Copy all worksheets in a file to an open workbook

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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 …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

822 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