+ Reply to Thread
Results 1 to 10 of 10

split and save individual tabs

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-18-2012
    Location
    New York
    MS-Off Ver
    2016
    Posts
    193

    split and save individual tabs

    Hello,

    I have been piecing together the below macro to walk through a file, paste over as values and save off each tab as its own file. It works exactly as I had hoped when I was testing it out in a generic file but when I bring it in with my live data, it is no longer working. I'm getting a debug error and it's highlighting the ActiveSheet.Select after the paste special lines. Any idea what could be causing this and is there a better way for me to do it?

    Thank you in advance for any help.

    Tristan

    Sub SplitWorkbook()
    '
    ' SplitWorkbook Macro
    '
    
    '
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        Cells.Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
        ActiveSheet.Select
        ActiveSheet.Copy
        Dim FileName As String
        Dim Path As String
        Application.DisplayAlerts = False
        Path = "C:\Users\jsmith\OneDrive - Test\Documents\Model\2022 Budget\Partners\"
        FileName = Range("A1").Value & ".xlsx"
        ActiveWorkbook.saveas Path & FileName, xlOpenXMLWorkbook
        Application.DisplayAlerts = True
        ActiveWorkbook.Close
        Next ws
    
    End Sub
    TestSplit.xlsx

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,700

    Re: split and save individual tabs

    ActiveSheet is the sheet currently open and showing. There should be no need to select it.
    What is "live" data?
    Last edited by jolivanes; 01-18-2022 at 07:48 PM.

  3. #3
    Forum Contributor GWteB's Avatar
    Join Date
    12-13-2021
    Location
    GMT +1
    MS-Off Ver
    2013
    Posts
    136

    Re: split and save individual tabs

    In general, there is no need to select or activate objects, so a corresponding run-time error is easily avoided in this regard.
    Try the code below and see if it does what you want.
    Sub SplitWorkbook()
    
        Dim ShtSource As Worksheet, ShtDestination As Worksheet
    
        For Each ShtSource In ThisWorkbook.Worksheets
    
            ShtSource.Copy
            Set ShtDestination = ActiveSheet
            With ShtDestination.UsedRange
                .Copy
                .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
            Excel.Application.CutCopyMode = False
            Excel.Application.Goto Range("A1")
    
            Dim FileName As String
            Dim Path As String
            Excel.Application.DisplayAlerts = False
            Path = "C:\Users\jsmith\OneDrive - Test\Documents\Model\2022 Budget\Partners\"
            With ShtDestination
                FileName = .Range("A1").Value & ".xlsx"
                .Parent.SaveAs Path & FileName, xlOpenXMLWorkbook
                .Parent.Close
            End With
            Excel.Application.DisplayAlerts = True
    
        Next ShtSource
    End Sub

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,700

    Re: split and save individual tabs

    I see you have a suggestion already.
    Since I was working on it, I might as well give mine also.
    Sub Maybe_So()
    Dim Path As String, ws As Worksheet
    Path = "C:\Users\jsmith\OneDrive - Test\Documents\Model\2022 Budget\Partners\"
        For Each ws In ThisWorkbook.Worksheets
        ws.Copy
            With ActiveWorkbook
                .Sheets(1).UsedRange.Value = .Sheets(1).UsedRange.Value
                    .SaveAs Path & .Sheets(1).Range("A1").Value & ".xlsx", FileFormat:=51
                .Close
            End With
        Next ws
    End Sub

  5. #5
    Forum Contributor
    Join Date
    07-18-2012
    Location
    New York
    MS-Off Ver
    2016
    Posts
    193

    Re: split and save individual tabs

    Thank you all for your quick responses.

    What I meant by live date is that I'm working with a file that has links to TM1 to pull financial data, I obviously should have mentioned that above. Upon further investigation, it sounds like that could be part of my problem. Unfortunately neither of the above solutions are working. I stepped through both and it doesn't look like they're actually copying the data over to the new tab which is I think what's causing the error when it tries to save because the filename is pulling from whatever values are in A1.

    I'm currently playing around with making a copy of my file and then using the TM1 snapshot function to break the links in the hopes that breaking the connection with TM1 might make a difference.

    Let me know if you have any other ideas.

    Thank you again.

  6. #6
    Forum Contributor
    Join Date
    07-18-2012
    Location
    New York
    MS-Off Ver
    2016
    Posts
    193

    Re: split and save individual tabs

    Thank you all again for your help,

    Going on the assumption that TM1 was the cause of my issue, I updated the file, broke the links using the snapshot function, saved the file as a copy and then disconnected TM1. I then reopened the file and ran my original macro and it worked.

    Enjoy the rest of the we

  7. #7
    Forum Contributor
    Join Date
    07-18-2012
    Location
    New York
    MS-Off Ver
    2016
    Posts
    193

    Re: split and save individual tabs

    It would seem that I spoke to soon. It seems to work intermittently. Going to continue to troubleshoot in the hopes that I can isolate what's causing the issue.

  8. #8
    Forum Contributor
    Join Date
    07-18-2012
    Location
    New York
    MS-Off Ver
    2016
    Posts
    193

    Re: split and save individual tabs

    I think it's the TM1 connection. I opened the VBA editor and there's an object "Sheet1 (Cognos_Office_Connection_Cache)". I'm going to try breaking the links and then bring what I need into a fresh file without that and then run the macro.

  9. #9
    Forum Contributor GWteB's Avatar
    Join Date
    12-13-2021
    Location
    GMT +1
    MS-Off Ver
    2013
    Posts
    136

    Re: split and save individual tabs

    Glad to help & thanks for the follow-up. If you're getting stuck, do let us know.

  10. #10
    Forum Contributor
    Join Date
    07-18-2012
    Location
    New York
    MS-Off Ver
    2016
    Posts
    193

    Re: split and save individual tabs

    Thank you,

    It definitely seems like the issue is the link with TM1. I took more of a brute force approach and it seems to work. I first ran

    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        Cells.Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Next ws
    to break all links in the entire file. I then tried my initial macro with no success. I then highlighted multiple tabs and did a move/copy to new book to create a fresh workbook with absolutely no TM1 connection, reran my initial macro and it worked.

    So far, all signs point to the TM1 connection being the issue.

+ 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] Split worksheet into individual tabs or into workbooks
    By LittleFry in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-24-2021, 02:02 PM
  2. [SOLVED] MACRO: Save all tabs, between two predifined tabs, individually as pdf files
    By bridge4444 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-01-2021, 01:17 AM
  3. [SOLVED] Macro to split tabs and save as individual textbooks
    By mark888 in forum Excel Programming / VBA / Macros
    Replies: 26
    Last Post: 06-08-2017, 05:52 AM
  4. Split Sheet 1 To Individual Worksheets
    By jo15765 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-30-2016, 11:36 PM
  5. VBA to save tabs as individual workbooks by tab's name
    By mfirpo in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-02-2014, 04:34 PM
  6. Split in to Individual Files
    By FallingDown in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-19-2013, 08:18 AM
  7. How Do I split a file into several Tabs, acording to information on other tabs?
    By Edinson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-12-2013, 05:17 AM

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