[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Copy all worksheets in a file to an open workbook

Posted on 2011-09-29
2
Medium Priority
?
205 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
[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
2 Comments
 
LVL 35

Accepted Solution

by:
Norie earned 2000 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

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!

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

656 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