+ Reply to Thread
Results 1 to 19 of 19

Split an Excel file into Multiple Sheets Based on a Specific Column

Hybrid View

  1. #1
    Registered User
    Join Date
    05-17-2019
    Location
    Ro
    MS-Off Ver
    2016
    Posts
    22

    Split an Excel file into Multiple Sheets Based on a Specific Column

    Hi guys,

    I need to split a excel file in multiple sheets based on column W. Every sheets must be named based on split criteria.
    After that I want to save the resulting sheets in a specific location.
    Thx

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2504
    Posts
    13,642

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    Some suggestions you may fund useful:
    1. Our answerers are looking to "help" more often than they are looking to "do an entire project for you for free".
    2. Jump into this project yourself, when you get stuck on ONE specific hurdle, post that ONE item as a forum question.
    3. Show us what you've tried in an attached sample workbook and how it's not working for you. When you're doing the work yourself, just writing out a complete example question in a forum often points you to solution before you even finish posting.
    4. If you can't make any headway at all and no help is forthcoming in the free forum(s), we do have a Commercial Services forum where you can pay for assistance directly from our EF gurus."

  3. #3
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    It would be easier to help and test possible solutions if you could attach a copy of your file. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary). See the yellow banner at the top of this page for instructions to attach a file. Do you want each new sheet to be saved as a separate file? What is the full path to the folder where you want to save the files?
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  4. #4
    Registered User
    Join Date
    05-17-2019
    Location
    Ro
    MS-Off Ver
    2016
    Posts
    22

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    Thank you for your reply.
    I can't provide the file, contains some confidential data but I can share my work so far (you will find it below). My approach was to filter the column based on specific criteria, copy filtered cells to another sheet, save that sheet to a specific location, then clear filters and repeat the procedure for another filter criteria.
    I am stuck when one of the filter criteria is missing, I don't know how to skip part of the code when a certain filter criteria is missing.
    How to skip this part of the code when filter criteria NCRT is missing in column W

    Range("W1").AutoFilter 23, "=NCRT"
        Cells.Select
        Selection.Copy
        Sheets.Add After:=ActiveSheet
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("A:A").EntireColumn.AutoFit
        Cells.Select
        Cells.EntireColumn.AutoFit
        Range("B1").Select
        
    Set Target = Range("W2")
    
    Application.Sheets(2).Name = VBA.Left(Target, 31)
    SaveNameA = Sheets(2).Range("W2").Text
    ActiveWorkbook.Sheets(2).Columns("W:W").Delete
    ActiveWorkbook.Sheets(2).Copy
    Application.ActiveWorkbook.SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & SaveNameA & ".xlsx"
    ActiveWorkbook.Close savechanges:=False
    Application.DisplayAlerts = False
    ActiveWorkbook.Sheets(2).Delete
    Application.DisplayAlerts = True



    My entire code

    Sub MyProject()
    
    Dim SaveNameA As String
    
    
    
    
    
    Range("W1").AutoFilter 23, "=NCRT"
        Cells.Select
        Selection.Copy
        Sheets.Add After:=ActiveSheet
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("A:A").EntireColumn.AutoFit
        Cells.Select
        Cells.EntireColumn.AutoFit
        Range("B1").Select
        
    Set Target = Range("W2")
    
    Application.Sheets(2).Name = VBA.Left(Target, 31)
    SaveNameA = Sheets(2).Range("W2").Text
    ActiveWorkbook.Sheets(2).Columns("W:W").Delete
    ActiveWorkbook.Sheets(2).Copy
    Application.ActiveWorkbook.SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & SaveNameA & ".xlsx"
    ActiveWorkbook.Close savechanges:=False
    Application.DisplayAlerts = False
    ActiveWorkbook.Sheets(2).Delete
    Application.DisplayAlerts = True
    
    If Sheet1.AutoFilterMode Then Sheet1.AutoFilterMode = False
    
    Range("W1").AutoFilter 23, "=INT"
        Cells.Select
        Selection.Copy
        Sheets.Add After:=ActiveSheet
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("A:A").EntireColumn.AutoFit
        Cells.Select
        Cells.EntireColumn.AutoFit
        Range("B1").Select
        
    Set Target = Range("W2")
    
    Application.Sheets(2).Name = VBA.Left(Target, 31)
    SaveNameA = Sheets(2).Range("W2").Text
    ActiveWorkbook.Sheets(2).Columns("W:W").Delete
    ActiveWorkbook.Sheets(2).Copy
    Application.ActiveWorkbook.SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & SaveNameA & ".xlsx"
    ActiveWorkbook.Close savechanges:=False
    Application.DisplayAlerts = False
    ActiveWorkbook.Sheets(2).Delete
    Application.DisplayAlerts = True
    
    If Sheet1.AutoFilterMode Then Sheet1.AutoFilterMode = False
    
    Range("W1").AutoFilter 23, "=REV"
        Cells.Select
        Selection.Copy
        Sheets.Add After:=ActiveSheet
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("A:A").EntireColumn.AutoFit
        Cells.Select
        Cells.EntireColumn.AutoFit
        Range("B1").Select
        
    Set Target = Range("W2")
    
    Application.Sheets(2).Name = VBA.Left(Target, 31)
    SaveNameA = Sheets(2).Range("W2").Text
    ActiveWorkbook.Sheets(2).Columns("W:W").Delete
    ActiveWorkbook.Sheets(2).Copy
    Application.ActiveWorkbook.SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & SaveNameA & ".xlsx"
    ActiveWorkbook.Close savechanges:=False
    Application.DisplayAlerts = False
    ActiveWorkbook.Sheets(2).Delete
    Application.DisplayAlerts = True
    
    If Sheet1.AutoFilterMode Then Sheet1.AutoFilterMode = False
    
      
        
    End Sub

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    It looks like your code can be shortened. Could you de-sensitize the confidential data and attach a copy of the file? You don't necessarily need to include all the data, just enough rows to make it exactly representative of your actual file. I think the solution can be simple but I can't test it unless I have a file to work with.

  6. #6
    Registered User
    Join Date
    05-17-2019
    Location
    Ro
    MS-Off Ver
    2016
    Posts
    22

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    I have attached the file.
    Thx.
    Attached Files Attached Files

  7. #7
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    Try:
    Sub MyProject()
        Application.ScreenUpdating = False
        Dim arr As Variant, i As Long
        arr = Array("NCRT", "INT", "REV")
        For i = LBound(arr) To UBound(arr)
            If WorksheetFunction.CountIf(Range("W:W"), arr(i)) > 0 Then
                With ActiveSheet
                    .Cells(1, 1).CurrentRegion.AutoFilter 23, arr(i)
                    .AutoFilter.Range.Copy
                    Sheets.Add After:=ActiveSheet
                    Cells(1, 1).PasteSpecial xlPasteValues
                    Cells.EntireColumn.AutoFit
                    Range("B1").Select
                    With ActiveSheet
                        .Name = Left(arr(i), 31)
                        .Columns("W:W").Delete
                        .Copy
                        With ActiveWorkbook
                            .SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & arr(i) & ".xlsx"
                            .Close False
                        End With
                    End With
                    Application.DisplayAlerts = False
                    Sheets(2).Delete
                    Application.DisplayAlerts = True
                End With
            End If
        Next i
        Range("A1").AutoFilter
        Application.ScreenUpdating = False
    End Sub
    If you need to add another filter criteria, simply add it to the array in the code.

  8. #8
    Registered User
    Join Date
    05-17-2019
    Location
    Ro
    MS-Off Ver
    2016
    Posts
    22

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    Quote Originally Posted by Mumps1 View Post
    Try:
    Sub MyProject()
        Application.ScreenUpdating = False
        Dim arr As Variant, i As Long
        arr = Array("NCRT", "INT", "REV")
        For i = LBound(arr) To UBound(arr)
            If WorksheetFunction.CountIf(Range("W:W"), arr(i)) > 0 Then
                With ActiveSheet
                    .Cells(1, 1).CurrentRegion.AutoFilter 23, arr(i)
                    .AutoFilter.Range.Copy
                    Sheets.Add After:=ActiveSheet
                    Cells(1, 1).PasteSpecial xlPasteValues
                    Cells.EntireColumn.AutoFit
                    Range("B1").Select
                    With ActiveSheet
                        .Name = Left(arr(i), 31)
                        .Columns("W:W").Delete
                        .Copy
                        With ActiveWorkbook
                            .SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & arr(i) & ".xlsx"
                            .Close False
                        End With
                    End With
                    Application.DisplayAlerts = False
                    Sheets(2).Delete
                    Application.DisplayAlerts = True
                End With
            End If
        Next i
        Range("A1").AutoFilter
        Application.ScreenUpdating = False
    End Sub
    If you need to add another filter criteria, simply add it to the array in the code.

    Thank you very much Mumps1, It works perfectly I owe you

  9. #9
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    You are very welcome.

  10. #10
    Registered User
    Join Date
    05-17-2019
    Location
    Ro
    MS-Off Ver
    2016
    Posts
    22

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    Quote Originally Posted by Mumps1 View Post
    You are very welcome.
    Hi Mumps1,

    I really appreciated your help with my request. I have another question, it is possible, adapting the script from you, to split the resulting files based on the currency?. The currency can be EUR, CHF, USD and will always be in column G (the currency may differ from file to file, in one file it may be only CHF and EUR, in another file it may be CHF, EUR and USD).
    The resulting files I would like to be named like this: "INT EUR", "INT CHF" and so on. The files I want to be saved in C:\Users\Home\Desktop\My Folder\Currency. I updated the test file with currency in column G.
    THX
    Attached Files Attached Files

  11. #11
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    Should it be new files or new sheets?

    Sub VenA()
      c00 = "E:\Temp\"
      Application.ScreenUpdating = False
      With Sheets("W").Cells(1).CurrentRegion
        .Columns(23).AdvancedFilter xlFilterCopy, , .Parent.Range("Z1"), True
        ar = .Parent.Cells(1, 26).CurrentRegion
        .Parent.Cells(1, 26).CurrentRegion.Clear
        If IsArray(ar) Then
          For j = 2 To UBound(ar)
            .AutoFilter 23, ar(j, 1)
            .Copy
            With Workbooks.Add.Sheets(1)
              .Paste
              .Name = Left(ar(j, 1), 23)
              .Columns.AutoFit
              .Columns(23).Delete
              .Parent.SaveAs c00 & ar(j, 1), 51
              .Parent.Close 0
            End With
          Next j
          .AutoFilter
        End If
      End With
    End Sub

  12. #12
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    Do you still want to split and save the data based on "NCRT", "INT", "REV" and then split each resulting file based on currency or just split and save on currency without splitting on "NCRT", "INT", "REV"?

  13. #13
    Registered User
    Join Date
    05-17-2019
    Location
    Ro
    MS-Off Ver
    2016
    Posts
    22

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    Quote Originally Posted by Mumps1 View Post
    Do you still want to split and save the data based on "NCRT", "INT", "REV" and then split each resulting file based on currency or just split and save on currency without splitting on "NCRT", "INT", "REV"?
    I want to split and save the data based on "NCRT", "INT", "REV" and then split each resulting file based on currency.

  14. #14
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    Try:
    Sub MyProject()
        Application.ScreenUpdating = False
        Dim arr1 As Variant, arr2 As Variant, i As Long, ii As Long
        arr1 = Array("NCRT", "INT", "REV")
        arr2 = Array("EUR", "CHF", "USD")
        For i = LBound(arr1) To UBound(arr1)
            If WorksheetFunction.CountIf(Range("W:W"), arr1(i)) > 0 Then
                With ActiveSheet
                    .Cells(1, 1).CurrentRegion.AutoFilter 23, arr1(i)
                    .AutoFilter.Range.Copy
                    Sheets.Add After:=ActiveSheet
                    Cells(1, 1).PasteSpecial xlPasteValues
                    Cells.EntireColumn.AutoFit
                    Range("B1").Select
                    With ActiveSheet
                        .Name = Left(arr1(i), 31)
                        .Columns("W:W").Delete
                        .Copy
                        With ActiveWorkbook
                            Application.DisplayAlerts = False
                            .SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & arr1(i) & ".xlsx"
                            Application.DisplayAlerts = True
                            With .Sheets(Left(arr1(i), 31))
                                For ii = LBound(arr2) To UBound(arr2)
                                    If WorksheetFunction.CountIf(.Range("G:G"), arr2(ii)) > 0 Then
                                        .Cells(1, 1).CurrentRegion.AutoFilter 7, arr2(ii)
                                        .AutoFilter.Range.Copy
                                        Sheets.Add After:=ActiveSheet
                                        Cells(1, 1).PasteSpecial xlPasteValues
                                        Cells.EntireColumn.AutoFit
                                        Range("B1").Select
                                        .Name = Left(arr1(i), 31) & " " & arr2(ii)
                                        .Copy
                                        Application.DisplayAlerts = False
                                        ActiveWorkbook.SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & arr1(i) & " " & arr2(ii) & ".xlsx"
                                        ActiveWorkbook.Close False
                                        Sheets(2).Delete
                                        Application.DisplayAlerts = True
                                    End If
                                Next ii
                            End With
                        End With
                        ActiveWorkbook.Close False
                    End With
                    Application.DisplayAlerts = False
                    Sheets(2).Delete
                    Application.DisplayAlerts = True
                End With
            End If
        Next i
        Range("A1").AutoFilter
        Application.ScreenUpdating = False
    End Sub
    Last edited by Mumps1; 04-27-2020 at 11:38 AM.

  15. #15
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    It is not necessary to quote full messages. You apparently haven't looked at the code in # 8. This is much more efficient and dynamic.

    It is not necessary to always create a tab and delete it.

    Sub VenA()
    Dim c00 As String, j As Long, ar
      Application.ScreenUpdating = False
      c00 = "E:\Temp\Temp1\" '"C:\Users\Home\Desktop\My Folder\"
      With Sheets("W").Cells(1).CurrentRegion
        Sheets.Add.Name = "Temp"
        Cells(1).Resize(, 2) = Array("WWW", "CUU")
        .AdvancedFilter xlFilterCopy, , Range("A1:B1"), True
        ar = Cells(1).CurrentRegion
        Application.DisplayAlerts = False
        Sheets("Temp").Delete
        For j = 2 To UBound(ar)
          .AutoFilter 23, ar(j, 1)
          .AutoFilter 7, ar(j, 2)
          .Copy
          With Workbooks.Add.Sheets(1)
            .Paste
            .Name = Left(ar(j, 1), 23)
            .Columns.AutoFit
            .Columns(23).Delete
            .Parent.SaveAs c00 & ar(j, 1) & "_" & ar(j, 2), 51
            .Parent.Close 0
          End With
        Next j
        .AutoFilter
      End With
    End Sub
    Someone with more posts doesn't necessarily write better code

  16. #16
    Registered User
    Join Date
    05-17-2019
    Location
    Ro
    MS-Off Ver
    2016
    Posts
    22

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    Thanks guys, both solutions provided work very well. I am grateful to you.

  17. #17
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    The macro suggested by Vraag en antwoord is excellent. However, it doesn't create the workbooks based on "NCRT", "INT", "REV" without splitting based on currency.

  18. #18
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    @Mumps1,

    They seemed unnecessary to me. But of course it can be adjusted.

  19. #19
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,081

    Re: Split an Excel file into Multiple Sheets Based on a Specific Column

    I suppose that the OP will have to decide if he/she needs the other files.

+ 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 one excel sheet into multiple sheets based on column value but only selected columns
    By nr6281 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-01-2019, 05:25 AM
  2. Split Excel file into multiple Excel files based on specific column value
    By qiyusi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2018, 10:08 PM
  3. [SOLVED] Macro to split data onto new sheets based on specific column
    By pauldaddyadams in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-17-2015, 12:53 PM
  4. Split one Excel file into multiple files based on values in column A?
    By MetroBOS in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2015, 10:59 AM
  5. Split excel file having multipl sheets into multiple excel file based on column
    By Shaharyarwatto in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-22-2014, 05:02 AM
  6. [SOLVED] How To Split Excel File Into Separate Workbooks Based on Values In a Specific Column
    By UzieJacuzzi in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 07-31-2012, 07:26 AM
  7. Replies: 3
    Last Post: 08-02-2006, 12:35 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