+ Reply to Thread
Results 1 to 24 of 24

Splitting Worksheet Into Multiple Workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    12-09-2010
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    22

    Splitting Worksheet Into Multiple Workbooks

    Hi all,

    I need to split my worksheet into multiple workbooks based on column B and name the new workbook according to column B. For exampleCapture.JPG
    and save as SM-30403-0.xlsx and so on.

    I know there are many such examples around but my attempt to adapt from the examples gave me errors "runtime error 1004 - the file could not be accessed" trying to write the new workbooks. I would appreciate any help and advice as I am going round in circles getting nowhere.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    01-14-2013
    Location
    Austria
    MS-Off Ver
    2016 / 2019
    Posts
    339

    Re: Splitting Worksheet Into Multiple Workbooks

    Your problem is that you copy your data into the last column. Reading the data includes a carriage return and this is a character that cannot be handled.
    Copy your data to .Columns.Count - 1 and read it from there and it will work

    cheers

  3. #3
    Registered User
    Join Date
    12-09-2010
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Splitting Worksheet Into Multiple Workbooks

    Quote Originally Posted by gue2013 View Post
    Your problem is that you copy your data into the last column. Reading the data includes a carriage return and this is a character that cannot be handled.
    Copy your data to .Columns.Count - 1 and read it from there and it will work

    cheers
    Hi gue2013,

    Thank you for pointing out my errors. You are spot on. I have another workbook having similar codes which I noticed data in my last column even though it managed to split the workbook into multiple worksheets. Could you enlighten me about this >> "Copy your data to .Columns.Count - 1" as I do not quite understand what you meant.

    Cheers

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Splitting Worksheet Into Multiple Workbooks

    Sub INVOICES_SPLIT()
    Dim Data, Dict As Object, i As Long
    Application.ScreenUpdating = False
    Set Dict = CreateObject("scripting.dictionary")
    With Sheet1
        Data = .Cells(1).CurrentRegion
        For i = 2 To UBound(Data)
            If Not Dict.exists(Data(i, 2)) Then
                Dict.Add Data(i, 2), 1
                With .Cells(1).CurrentRegion
                    .AutoFilter 2, Data(i, 2)
                    If Evaluate("ISREF('" & Data(i, 2) & "'!A1)") = False Then Sheets.Add(, Sheets(Sheets.Count)).Name = Data(i, 2)
                    .SpecialCells(12).Copy Sheets(Data(i, 2)).Range("A1")
                    .AutoFilter
                End With
            End If
        Next i
    End With
    Application.ScreenUpdating = True
    End Sub
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Splitting Worksheet Into Multiple Workbooks

    (removed as too late)

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Splitting Worksheet Into Multiple Workbooks

    Oops I only now noticed new workbooks...
    Your file has an issue though where column B has carriage gap | Multi-line in cell...Hence the error to save workbook...
    See this upload with new add workbook code...
    Attached Files Attached Files
    Last edited by sintek; 05-01-2020 at 09:17 AM.

  7. #7
    Registered User
    Join Date
    12-09-2010
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Splitting Worksheet Into Multiple Workbooks

    Quote Originally Posted by sintek View Post
    Oops I only now noticed new workbooks...
    Your file has an issue though where column B has carriage gap | Multi-line in cell...Hence the error to save workbook...
    See this upload with new add workbook code...
    Hi Sintek,
    Thank you! It works exactly what I needed.. Appreciate that you pointed out the column B (carriage gap/Multi-line) error. This worksheet was converted to excel from another format, hence the formatting problem for me.

  8. #8
    Valued Forum Contributor
    Join Date
    01-14-2013
    Location
    Austria
    MS-Off Ver
    2016 / 2019
    Posts
    339

    Re: Splitting Worksheet Into Multiple Workbooks

    Sorry but I did some modifications to get rid of the LineFeed. Copying not to the latest columns would not solve your problems. This was a misinterpretation on my side

    the simpliest thing is to change your filename to
    filename = Replace(rfl.Text, vbLf, "") & ".xlsx"
    This won't change anything if a LF is not available and remove it if available

    cheers
    Last edited by gue2013; 05-02-2020 at 06:47 AM.

  9. #9
    Registered User
    Join Date
    12-09-2010
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Splitting Worksheet Into Multiple Workbooks

    Hi all,
    As pointed out by gue2013 and sintek, there is a problem with my data has (carriage gap/Multi-line) error. I tried to remove the (carriage gap/Multi-line) in the data with vba but I keep getting runtime error 1004 when I try to split the workbook.

    I would appreciate if someone help me add codes into the macro to remove the (carriage gap/Multi-line) so it can run without runtime error?
    Attached Files Attached Files

  10. #10
    Valued Forum Contributor
    Join Date
    01-14-2013
    Location
    Austria
    MS-Off Ver
    2016 / 2019
    Posts
    339

    Re: Splitting Worksheet Into Multiple Workbooks

    you have 2 problems in your code
    1) you have to remove all special characters in your data (carriage return & line feeds, both are sometimes available. First you have to remove these chars (replace with "") and then you can select the region to store in an array ("Data")
    2) your loop is wrong. you first have to find all keys for your dictionary and then you can go through all keys and filter your data.

    I attached a working version

    cheers
    Attached Files Attached Files

  11. #11
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Splitting Worksheet Into Multiple Workbooks

    No need for two loops...Make use of the code in Post 5 sample and change only this line....

    Post not allowing me to add the code....
    Attached Files Attached Files
    Last edited by sintek; 05-05-2020 at 07:48 AM.

  12. #12
    Registered User
    Join Date
    12-09-2010
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Splitting Worksheet Into Multiple Workbooks

    Hi Sintek,
    Thank you very much for your quick response to my request. When I run the codes, I no longer get runtime error 1004 as it has taken care of the (carriage return & line feeds).
    But when I run the codes, it prompts me if I want to replace the split workbooks. If I choose replace, and for example (SM-3080163-0), there should be 7 rows in the newly created workbook, the final workbook I got only has 3 rows. So I guess maybe during filtering, it is splitting "SM-3080163-0" and "SM-3080163-0 " different workbooks and writing into same file name. Any advise on this? Cheers.

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Splitting Worksheet Into Multiple Workbooks


    Quote Originally Posted by sintek View Post

    Post not allowing me to add the code....
    Hi Sintek !

    Just add a $ character or a space to one of your Replace statement …

  14. #14
    Valued Forum Contributor
    Join Date
    01-14-2013
    Location
    Austria
    MS-Off Ver
    2016 / 2019
    Posts
    339

    Re: Splitting Worksheet Into Multiple Workbooks

    I did not execute your code but it seems to me that
    - dict.exists checks the uncorrected data
    - filtering is done on uncorrected data --> you will overwrite the file and only the last one is valid?

    With Sheet1
        .Cells(1).CurrentRegion.Replace What:=vbCr, Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        .Cells(1).CurrentRegion.Replace What:=vbLf, Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Data = .Cells(1).CurrentRegion
    Last edited by gue2013; 05-05-2020 at 03:26 AM.

  15. #15
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Splitting Worksheet Into Multiple Workbooks

    @gue2013...Good catch...My bad did not test...
    See small change...


    Anyway, as suggested in post above...Better to edit the actual data before so that it will be changed in the new files created...gue above code solves that...
    Attached Files Attached Files
    Last edited by sintek; 05-05-2020 at 04:15 AM.

  16. #16
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Splitting Worksheet Into Multiple Workbooks

    @ Marc L

    tried that...No avail...

  17. #17
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Splitting Worksheet Into Multiple Workbooks


    As it well works on my side :

    PHP Code: 
                File ThisWorkbook.Path "\" & Replace$(Replace(Data(i, 2), vbCr, ""), vbLf, ""): Dict.Add Data(i, 2), 1 

    Or :

    PHP Code: 
                File ThisWorkbook.Path "\" & Replace( Replace(Data(i, 2), vbCr, ""), vbLf, ""): Dict.Add Data(i, 2), 1 

  18. #18
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Splitting Worksheet Into Multiple Workbooks

    Nope...Not allowing code post...Anyway tx for time...

  19. #19
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Splitting Worksheet Into Multiple Workbooks


    You did something wrong as that's your post #11 code :

    Sub INVOICES_SPLIT()
    Dim Data, File As String, Dict As Object, i As Long
    Application.ScreenUpdating = False
    Set Dict = CreateObject("scripting.dictionary")
    With Sheet1
        Data = .Cells(1).CurrentRegion
        For i = 2 To UBound(Data)
            If Not Dict.exists(Data(i, 2)) Then
                File = ThisWorkbook.Path & "\" & Replace$(Replace(Data(i, 2), vbCr, ""), vbLf, ""): Dict.Add Data(i, 2), 1
                With .Cells(1).CurrentRegion
                    .AutoFilter 2, Data(i, 2)
                    .SpecialCells(12).Copy
                    With Workbooks.Add
                        .Sheets(1).Range("A1").PasteSpecial xlPasteAll
                        .SaveAs File & ".xlsx"
                        .Close False
                    End With
                    .AutoFilter
                End With
            End If
        Next i
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    End Sub

  20. #20
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Splitting Worksheet Into Multiple Workbooks

    Ha...Look at that...was clearly doing something wrong Marc L

    Sub INVOICES_SPLIT()
    Dim Data, File As String, Dict As Object, i As Long, Crit As String
    Application.ScreenUpdating = False
    Set Dict = CreateObject("scripting.dictionary")
    With Sheet1
        Data = .Cells(1).CurrentRegion
        For i = 2 To UBound(Data)
            Crit = Replace$(Replace(Data(i, 2), vbCr, ""), vbLf, "")
            If Not Dict.exists(Crit) Then
                File = ThisWorkbook.Path & "\" & Crit: Dict.Add Crit, 1
                With .Cells(1).CurrentRegion
                    .AutoFilter 2, Crit
                    .SpecialCells(12).Copy
                    With Workbooks.Add
                        .Sheets(1).Range("A1").PasteSpecial xlPasteAll
                        .SaveAs File & ".xlsx"
                        .Close False
                    End With
                    .AutoFilter
                End With
            End If
        Next i
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    End Sub
    Last edited by sintek; 05-05-2020 at 11:51 AM.

  21. #21
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Splitting Worksheet Into Multiple Workbooks

    Originally Posted by sintek
    Ha...Look at that...was clearly doing something wrong
    Now you catch this workaround but this xxxxxx Securi can raise other issues,
    recently I can't found any workaround for an easy Select Case block codelines !
    Last edited by AliGW; 05-05-2020 at 12:30 PM. Reason: Thinly-veiled obscenity removed.

  22. #22
    Registered User
    Join Date
    12-09-2010
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Splitting Worksheet Into Multiple Workbooks

    Hi guys,
    Thanks a lot. It is finally running as I wish. Saving me lots of time from having to trial and error. Am trying to learn from all these discussions here. Cheers.....

  23. #23
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Splitting Worksheet Into Multiple Workbooks


    Exactly the same for your post #14 code : just well amending each double Replace statement …

    Sub INVOICES_SPLIT()
    Dim Data, File As String, Dict As Object, i As Long
    Application.ScreenUpdating = False
    Set Dict = CreateObject("scripting.dictionary")
    With Sheet1
        Data = .Cells(1).CurrentRegion
        For i = 2 To UBound(Data)
            If Not Dict.exists(Replace( Replace(Data(i, 2), vbCr, ""), vbLf, "")) Then
                File = ThisWorkbook.Path & "\" & Replace( Replace(Data(i, 2), vbCr, ""), vbLf, ""): Dict.Add Replace( Replace(Data(i, 2), vbCr, ""), vbLf, ""), 1
                With .Cells(1).CurrentRegion
                    .AutoFilter 2, Replace( Replace(Data(i, 2), vbCr, ""), vbLf, "")
                    .SpecialCells(12).Copy
                    With Workbooks.Add
                        .Sheets(1).Range("A1").PasteSpecial xlPasteAll
                        .SaveAs File & ".xlsx"
                        .Close False
                    End With
                    .AutoFilter
                End With
            End If
        Next i
    End With
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    End Sub

  24. #24
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,347

    Re: Splitting Worksheet Into Multiple Workbooks

    recently I can't found any workaround for an easy Select Case block codelines
    Yes had that issue here...where we discussed it...

    @ wizz_wizz...Glad you sorted...tx for rep...
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

+ 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. Splitting multiple worksheet
    By sachin483 in forum Excel General
    Replies: 2
    Last Post: 02-06-2017, 12:05 AM
  2. Replies: 0
    Last Post: 06-23-2016, 06:55 PM
  3. Splitting files into multiple workbooks on the basis of specific column
    By kundanlal in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-09-2014, 03:47 PM
  4. [SOLVED] splitting worksheet into multiple worksheets
    By mmdecarl in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-17-2013, 02:35 PM
  5. Splitting 2K worksheet into multiple worksheets
    By abmayfield in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-18-2012, 04:28 PM
  6. Excel 2007 : splitting up worksheet into seperate workbooks.
    By GaidenFocus in forum Excel General
    Replies: 14
    Last Post: 11-03-2010, 01:44 PM
  7. Splitting One Worksheet into Two Workbooks
    By jsmmao in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-25-2010, 03:33 PM

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