+ Reply to Thread
Results 1 to 6 of 6

How to move several worksheets in one workbook to several different workbooks

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    How to move several worksheets in one workbook to several different workbooks

    Hello everyone. I have worksheet, named "105", that contains several worksheets. I would like to move those worksheets to several other workbooks, with various names. Please see the code below:

    Sub Disclosure_Generation()
        Const sFileInp1 As String = "105.xlsm"
        Const sFileInp2 As String = "Borrowing_Portfolio_Bonds.xlsm"
        Const sFileInp3 As String = "Borrowing_Portfolio_Swaps.xlsm"
        Const sFileInp4 As String = "Client_Operations.xlsm"
        Const sFileInp5 As String = "Other_Equity.xlsm"
        Dim sPath1      As String
        Dim sPath2      As String
        Dim wb1         As Workbook
        Dim wb2         As Workbook
        Dim wb3         As Workbook
        Dim wb4         As Workbook
        Dim wb5         As Workbook
          
        sPath1 = fPath & fDate & "_157\105_Reports\"
        sPath2 = fPath & fDate & "_157\157_Reports\IBRD_Disclosure\"
        
        Set wb2 = Workbooks.Open(sPath2 & sFileInp2)
        Set wb3 = Workbooks.Open(sPath2 & sFileInp3)
        Set wb4 = Workbooks.Open(sPath2 & sFileInp4)
        Set wb5 = Workbooks.Open(sPath2 & sFileInp5)
        Set wb1 = Workbooks.Open(sPath1 & sFileInp1)
        
        Sheets("Bonds").Move Before:=Workbooks("Borrowing_Portfolio_Bonds.xlsm"). _
            Sheets(1)
        Sheets("Bond_Details").Move Before:=Workbooks( _
            "Borrowing_Portfolio_Bonds.xlsm").Sheets(1) <---------- Error
            
        Sheets("Swaps").Move Before:=Workbooks("Borrowing_Portfolio_Swaps.xlsm"). _
            Sheets(1)
        Sheets("Swap_Details").Move Before:=Workbooks( _
            "Borrowing_Portfolio_Swaps.xlsm").Sheets(1)
        
        Sheets("Client_Operations").Move Before:=Workbooks("Client_Operations.xlsm"). _
            Sheets(1)
        Sheets("Client_Operations_Details").Move Before:=Workbooks( _
            "Client_Operations.xlsm").Sheets(1)
            
        Sheets("Other_Equity").Move Before:=Workbooks("Other_Equity.xlsm"). _
            Sheets(1)
        Sheets("Client_Operations_Details").Move Before:=Workbooks( _
            "Other_Equity.xlsm").Sheets(1)
            
        wb1.Close SaveChanges:=True
        wb2.Close SaveChanges:=True
        wb3.Close SaveChanges:=True
        wb4.Close SaveChanges:=True
        wb5.Close SaveChanges:=True
    
    End Sub
    I am getting an error, "subscript out of range", and I not sure of a correct fix. Any suggestions. Thank you.
    Last edited by AnthonyWB; 05-07-2010 at 03:50 PM.

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: How to move several worksheets in one workbook to several different workbooks

    This error is occuring because once you move a sheet to a different workbook, that workbook becomes the active workbook. Before you can move another sheet in your source workbook, you need to activate it. Does that make sense?

    I don't see the name of your source workbook, but before the line with the error you should put:

    Workbooks("Source Workbook Name").Activate
    You'll need to put that after each line of code that moves a sheet to a different workbook.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: How to move several worksheets in one workbook to several different workbooks

    I followed you suggestion, I now have an error in the code below:

        Workbooks("105.xlsm").Activate
        Sheets("Client_Operations_Details").Move Before:=Workbooks( _
            "Client_Operations.xlsm").Sheets(1)
    according to the intermediate window, I have a subsript which is out of range?

  4. #4
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: How to move several worksheets in one workbook to several different workbooks

    Subscript out of Range usually indicates you are refering to an object incorrectly. I'd check the spelling on each item in parantheses above.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to move several worksheets in one workbook to several different workbooks

    You should utilize all the variables you've declared throughout the entire macro to keep the code easy to to edit...you need only edit your strings at the top.

    For moving sheets, be sure to include the parent object wb1 as the source for the sheets you're trying to move.

    This is how I would adjust your macro:
    Option Explicit
    Const sFileInp1 As String = "105.xlsm"
    Const sFileInp2 As String = "Borrowing_Portfolio_Bonds.xlsm"
    Const sFileInp3 As String = "Borrowing_Portfolio_Swaps.xlsm"
    Const sFileInp4 As String = "Client_Operations.xlsm"
    Const sFileInp5 As String = "Other_Equity.xlsm"
    
    Sub Disclosure_Generation()
    Dim sPath1      As String
    Dim sPath2      As String
    Dim wb1         As Workbook
    Dim wb2         As Workbook
    Dim wb3         As Workbook
    Dim wb4         As Workbook
    Dim wb5         As Workbook
    Application.ScreenUpdating = False
    
    sPath1 = fPath & fDate & "_157\105_Reports\"
    sPath2 = fPath & fDate & "_157\157_Reports\IBRD_Disclosure\"
    
    Set wb2 = Workbooks.Open(sPath2 & sFileInp2)
    Set wb3 = Workbooks.Open(sPath2 & sFileInp3)
    Set wb4 = Workbooks.Open(sPath2 & sFileInp4)
    Set wb5 = Workbooks.Open(sPath2 & sFileInp5)
    Set wb1 = Workbooks.Open(sPath1 & sFileInp1)
        
        wb1.Sheets("Bonds").Move _
            Before:=Workbooks(sFileInp2).Sheets(1)
        wb1.Sheets("Bond_Details").Move _
            Before:=Workbooks(sFileInp2).Sheets(1)
        wb1.Sheets("Swaps").Move _
            Before:=Workbooks(sFileInp3).Sheets(1)
        wb1.Sheets("Swap_Details").Move _
            Before:=Workbooks(sFileInp3).Sheets(1)
        wb1.Sheets("Client_Operations").Move _
            Before:=Workbooks(sFileInp4).Sheets(1)
        wb1.Sheets("Client_Operations_Details").Move _
            Before:=Workbooks(sFileInp4).Sheets(1)
        wb1.Sheets("Other_Equity").Move _
            Before:=Workbooks(sFileInp5).Sheets(1)
        wb1.Sheets("Client_Operations_Details").Move _
            Before:=Workbooks(sFileInp5).Sheets(1)
                
        wb1.Close SaveChanges:=True
        wb2.Close SaveChanges:=True
        wb3.Close SaveChanges:=True
        wb4.Close SaveChanges:=True
        wb5.Close SaveChanges:=True
    
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  6. #6
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: How to move several worksheets in one workbook to several different workbooks

    Yep you were right, Thanks !

+ 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