+ Reply to Thread
Results 1 to 3 of 3

Do While Loop to Import Worksheets from Various Workbooks

  1. #1
    Registered User
    Join Date
    07-22-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    85

    Do While Loop to Import Worksheets from Various Workbooks

    Hi, I am fairly new to macros and have a problem. I want to import specific worksheets from various workbooks. The file destination can be found on col B and the worksheet name can be found on col C. So far I have come up with the formula below but it doesn't seem to work. I have attached the Main(which is the destination workbook) and 2 Test workbooks where the worksheets have to be attracted from. I would like to thank anyone who is helping me in advance

    Sub GrabWBs()

    Dim x As Integer


    x = 2

    Do While Cells(x, 1) <> ""

    Workbooks.Open Filename:=Cells(x, 2)
    Worksheets(Cells(x, 3)).Select
    Worksheets(Cells(x, 3)).Copy After:=Workbooks("Main.xlsm").Sheets("Control")
    Workbooks(Cells(x, 2)).Select
    ActiveWorkbooks.Close

    x = x + 1

    Loop



    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Do While Loop to Import Worksheets from Various Workbooks

    kchm_2000,

    Right away I noticed some problems in the setup. Your code looks if Cell(x,1) is empty. But you don't have any data in column A in the Main workbook, so the first cell it looks at is empty and then it exits the macro. Also, the sheet you wanted from Test2.xlsx didn't exist. It looks like Test.xlsx was just copy/pasted wholesale, so they both have a worksheet named "1" and Test2 did not have a worksheet named "2" which was specified in the Main workbook.

    I renamed the "1" worksheet in Test2 to "2" so that it matched what the Main workbook was searching for, and then I updated your code:
    Please Login or Register  to view this content.


    Hope that helps,
    ~tigeravatar

  3. #3
    Registered User
    Join Date
    07-22-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    85

    Re: Do While Loop to Import Worksheets from Various Workbooks

    Wow thanks, I think i got a little careless. But is there a way to make the program more robust such that if it detects that there is already a worksheet (1) in the destination spreadsheet, it would delete it and replace it with the one from the copied one? So far the code is working wonderfully thanks a bunch

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1