+ Reply to Thread
Results 1 to 14 of 14

Splitting a workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    10-01-2015
    Location
    DC
    MS-Off Ver
    2013 Pro Plus
    Posts
    22

    Exclamation Splitting a workbook

    Hi,

    I have a workbook with 27 sheets where 4 (A, B, C, D) of them are reference sheets and the rest are reporting sheets (1-23). I'd like to create 23 individual workbooks that consist of 4 reference sheets A, B, C, D and 1 reporting sheet. Sheet names for reporting sheets are stored in the named range "Subs"
    Here is the code I am trying to use but it generates "Subscript out of Range" Error 9.


     Sub CopyPasteComboTabs()
    
          Dim strSaveName As String
          Dim xPath As String
          
            xPath = Application.ActiveWorkbook.Path
            Application.ScreenUpdating = False
            Application.DisplayAlerts = False
        
        For Each c In Range("Subs")
          strSaveName = c.Value
          
        Sheets(strSaveName).Activate    
        Sheets(Array("A", "B", "C", "D", strSaveName)).Select
        Sheets(Array("A", "B", "C", "D", strSaveName)).Copy
          
    Next
        Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & strSaveName & ".xls"
        Application.ActiveWorkbook.Close True
        
    End Sub
    Please Help me understand why it would not work?
    Last edited by gilliamwibson; 02-01-2016 at 03:17 PM. Reason: code tags

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Splitting a workbook

    How are the reporting sheets named?

    PS Can you add code tags when posting code?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    10-01-2015
    Location
    DC
    MS-Off Ver
    2013 Pro Plus
    Posts
    22

    Re: Splitting a workbook

    Hi Norie,

    Thank you for responding. The tab names are just names of countries like Russia, Haiti etc..

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Splitting a workbook

    Are there any other workbooks open?

    Does this work?

    Sub CopyPasteComboTabs()
    Dim wbSource As Workbook
    Dim strSaveName As String
    Dim xPath As String
    Dim c As Range
    
        Set wbSource = ActiveWorkbook
        xPath = wbSource.Path
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
    
        For Each c In wbSource.Range("Subs") ' mind need sheet reference for 'Subs'
            
            strSaveName = c.Value
    
            wbSource.Sheets(Array("A", "B", "C", "D", strSaveName)).Copy
            ActiveWorkbook.SaveAs Filename:=xPath & "\" & strSaveName & ".xls"
            ActiveWorkbook.Close True
            
        Next c
            
    End Sub

  5. #5
    Registered User
    Join Date
    10-01-2015
    Location
    DC
    MS-Off Ver
    2013 Pro Plus
    Posts
    22

    Re: Splitting a workbook

    Thank you but it didn't. and no other workbooks were open.
    Run-time error '91': Object variable or With block variable not set.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Splitting a workbook

    Where did you get the error?

    Did you try adding a worksheet reference for Range("Subs")?

  7. #7
    Registered User
    Join Date
    10-01-2015
    Location
    DC
    MS-Off Ver
    2013 Pro Plus
    Posts
    22

    Re: Splitting a workbook

    xPath = wbSource.Path
    Subs is a global named range and I am copying the tab where the ranges are located (i.e. Lists tab) together with other tabs into the split books.

  8. #8
    Registered User
    Join Date
    10-01-2015
    Location
    DC
    MS-Off Ver
    2013 Pro Plus
    Posts
    22

    Re: Splitting a workbook

    the code I sent you initially worked perfectly before, idk what changed..

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Splitting a workbook

    The code works fine for me in a mocked up workbook if I change the For statement to this.
    For Each c In Range("Subs")
    PS In the code that fails how is xPath declared?

  10. #10
    Registered User
    Join Date
    10-01-2015
    Location
    DC
    MS-Off Ver
    2013 Pro Plus
    Posts
    22

    Re: Splitting a workbook

    Dim xPath As String

  11. #11
    Registered User
    Join Date
    10-01-2015
    Location
    DC
    MS-Off Ver
    2013 Pro Plus
    Posts
    22

    Re: Splitting a workbook

    I just tried the For loop the way you showed it gave the same run-time error '91'

    Also please note that I am running this code from a module so idk if that makes any difference. will report as soon as I try on ThisWorkbook

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Splitting a workbook

    The code should be run from a module, though running it from ThisWorkbook shoudn't make any difference.

    As for the error you describe, the only thing I can think that might cause that would be that xPath was incorrectly declared, or there wasn't an active workbook open, both of which seem highly unlikely.

    Any chance you could upload a sample workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    PS No need for data other than that in the 'Subs' list.

  13. #13
    Registered User
    Join Date
    10-01-2015
    Location
    DC
    MS-Off Ver
    2013 Pro Plus
    Posts
    22

    Re: Splitting a workbook

    I had a typo!!!! so sorry!
    Instead of source I wrote sourse. Because I always rewrite things to remember instead of copy&paste so i fucked it up.
    In short it worked perfectly!
    Thank you a lot.

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Splitting a workbook

    No problem.

    PS A little tip to avoid that type of thing, add Option Explicit at the top of each module. That will force you to declare all variables which might be a little annoying at first but it'll also pick up typos.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Splitting workbook
    By Roadhouse in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-04-2015, 06:13 PM
  2. Splitting Workbook into multiple books
    By Uther_d_dragon in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-22-2013, 10:54 AM
  3. Splitting data into new workbook
    By Apsank1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-13-2012, 09:14 PM
  4. Splitting data into many workbook using a ID
    By a.hussain in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-22-2012, 02:49 PM
  5. Workbook Sharing and Text Splitting
    By rakesh14021983 in forum Excel General
    Replies: 3
    Last Post: 01-10-2009, 11:54 AM
  6. [SOLVED] Splitting a workbook
    By Tony Vella in forum Excel General
    Replies: 2
    Last Post: 08-14-2006, 04:55 PM
  7. [SOLVED] Splitting a workbook
    By MarkN in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-19-2006, 01:20 AM

Tags for this Thread

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