Solved

Copy all worksheets in a file to an open workbook

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
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 …

762 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

19 Experts available now in Live!

Get 1:1 Help Now