+ Reply to Thread
Results 1 to 23 of 23

Splitting data into seperate spreadsheets based on column values

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Splitting data into seperate spreadsheets based on column values

    Hi,
    I have a data that has several columns. But I need to separate the spread sheet based on one particular column values.Please help.

    Thanks,

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Splitting data into seperate spreadsheets based on column values

    Need a bit more detail than that.

    Can you attach a workbook, and explain exactly what you are trying to achieve, based on which conditions?
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Splitting data into seperate spreadsheets based on column values

    here's one approach
    the cl& = 2 near the top of the code says to use Column2 for your splitting
    you can change this to any column you like.
    Sub col_to_sheets()
    
    Const cl& = 2
    Dim a As Variant, x As Worksheet, sh As Worksheet
    Dim rws&, cls&, p&, i&, rr&, b As Boolean
    Application.ScreenUpdating = False
    Sheets("sheet1").Activate
    rws = Cells.Find("*", , , , xlByRows, xlPrevious).Row
    cls = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
    Sheets.Add(After:=Sheets("Sheet1")).Name = Chr(30)
    Set x = Sheets(Chr(30))
        Sheets("Sheet1").Cells(1).Resize(rws, cls).Copy x.Cells(1)
        Set a = x.Cells(1).Resize(rws, cls)
        a.Sort a(1, cl), 2, Header:=xlYes
        a = a.Resize(rws + 1)
        p = 2
        For i = p To rws + 1
            If a(i, cl) <> a(p, cl) Then
                b = False
                For Each sh In Worksheets
                    If sh.Name = a(p, cl) Then b = True: Exit For
                Next
                If Not b Then Sheets.Add.Name = a(p, cl)
                With Sheets(a(p, cl))
                    x.Cells(1).Resize(, cls).Copy .Cells(1)
                    rr = .Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1
                    x.Cells(p, 1).Resize(i - p, cls).Cut .Cells(rr, 1)
                End With
                p = i
            End If
        Next i
    Application.DisplayAlerts = False
        Sheets(Chr(30)).Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
    End Sub

  4. #4
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Splitting data into seperate spreadsheets based on column values

    I have several columns in the spreadsheet and I need to split the spread sheet based on any clolumn values.It could be 1,2,...or n...
    For Example
    Clo1 col2 col3 col4
    1 34 45 56
    3 45 76 67
    5 64 58 89

    If I consider col2 values I need to pot each value for example 34,45,64 in different spread sheets.

    Thanks

  5. #5
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Splitting data into seperate spreadsheets based on column values

    The above code creates sheets named "34", "45" and "58" if Col2 is used, and if sheets with those names don't exist already.

    If that's not what you want, could you be more specific about just what it is that you do want?

  6. #6
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Splitting data into seperate spreadsheets based on column values

    Hi,

    I need to separate the spread sheet based on any column values I select.
    For example: If I select column say Country that contains values IND,USA,CHN,ESP. I want separate the main sheet into sub sheets based on these values.

    Thanks

  7. #7
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Splitting data into seperate spreadsheets based on column values

    have your data on sheet1
    select a cell in the column you want to operate with and run this macro
    Sub sheets_from_colvalues()
    
    Const sh1 As String = "Sheet1"
    Dim a As Variant, x As Worksheet, sh As Worksheet
    Dim rws&, cls&, p&, i&, rr&, b As Boolean
    Dim cl&
    cl = Selection.Column
    Application.ScreenUpdating = False
    Sheets(sh1).Activate
    rws = Cells.Find("*", , , , xlByRows, xlPrevious).Row
    cls = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
    Set x = Sheets.Add(After:=Sheets(sh1))
    Sheets(sh1).Cells(1).Resize(rws, cls).Copy x.Cells(1)
    Set a = x.Cells(1).Resize(rws, cls)
    a.Sort a(1, cl), 2, Header:=xlYes
    a = a.Resize(rws + 1)
        p = 2
        For i = p To rws + 1
            If a(i, cl) <> a(p, cl) Then
                b = False
                For Each sh In Worksheets
                    If sh.Name = a(p, cl) Then b = True: Exit For
                Next
                If Not b Then
                    With Sheets.Add
                        .Name = a(p, cl)
                        x.Cells(1).Resize(, cls).Copy .Cells(1)
                        rr = .Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1
                        x.Cells(p, 1).Resize(i - p, cls).Cut .Cells(rr, 1)
                        .Columns.AutoFit
                    End With
                End If
                p = i
            End If
        Next i
    Application.DisplayAlerts = False
        x.Delete
    Application.DisplayAlerts = True
    Sheets(sh1).Activate
    Application.ScreenUpdating = True
    End Sub

  8. #8
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Splitting data into seperate spreadsheets based on column values

    Thanks but why it is not separating if the value is 0 in the column?

    Thanks

  9. #9
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Splitting data into seperate spreadsheets based on column values

    Hi,

    I this code not splitting data if the value is 0.

    Thanks,
    Narendra

  10. #10
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Splitting data into seperate spreadsheets based on column values

    Hi,

    I need to create Ribbon Galleries for this code and need to name the main menu as 'validate values' and sub menu as 'split sheets'please help me on this.

    Thanks,
    Narendra

  11. #11
    Valued Forum Contributor
    Join Date
    03-21-2013
    Location
    cyberia
    MS-Off Ver
    Excel 2007
    Posts
    457

    Re: Splitting data into seperate spreadsheets based on column values

    regarding posts #8 and #9, try
    Sub col_to_sheetz()
    
    Const cl& = 1
    Dim lr&, lc&, s7, i&
    Dim hdr, q As String, sh As Worksheet
    
    Application.ScreenUpdating = False
    lr = Cells.Find("*", , , , xlByRows, xlPrevious).Row
    lc = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
    s = 2
    Set ash = ActiveSheet
    
    With Sheets.Add(after:=ash)
        ash.Cells(1).Resize(lr, lc).Copy .Cells(1)
        hdr = .Cells(1).Resize(, lc)
        .Cells(1).Resize(lr, lc).Sort .Cells(cl), Header:=xlYes
        a = .Cells(cl).Resize(lr + 1)
        For i = 2 To lr
            If a(i, 1) <> a(i + 1, 1) Then
                q = CStr(a(i, 1))
                On Error Resume Next
                Set sh = Sheets(q)
                On Error GoTo 0
                If sh Is Nothing Then
                    Sheets.Add(after:=Sheets(Sheets.Count)).Name = q
                    .Cells(s, 1).Resize(i - s + 1, lc).Copy Sheets(q).Cells(2, 1)
                    s = i + 1
                    Sheets(q).Cells(1).Resize(, lc) = hdr
                 End If
            End If
        Next i
    Application.DisplayAlerts = False
        .Delete
    Application.DisplayAlerts = True
    End With
    
    ash.Activate
    Application.ScreenUpdating = True
    End Sub

  12. #12
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Splitting data into seperate spreadsheets based on column values

    Hi,

    I need to create Ribbon Galleries for this code and need to name the main menu as 'validate values' and sub menu as 'split sheets' please help me on this.

    Thanks,
    Narendra

  13. #13
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Splitting data into seperate spreadsheets based on column values

    Hi,

    I need to create Ribbon Galleries for this code and need to name the main menu as 'validate values' and sub menu as 'split sheets' please help me on this.
    It should not come under add ins tab.
    Thanks,
    Narendra

  14. #14
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Splitting data into seperate spreadsheets based on column values

    Hi,

    I am getting cannot rename a sheet with the same name if the values in the column are like A and a.It is taking uppercase and lower case case as different values.

    Thanks!

  15. #15
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Splitting data into seperate spreadsheets based on column values

    One more solution.
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

  16. #16
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Splitting data into seperate spreadsheets based on column values

    Here you go, test this file and if you are satisfied save it as personal.xlsb file in

    C:\Documents and Settings\Your Login Name\Application Data\Microsoft\Excel\XLSTART

  17. #17
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Splitting data into seperate spreadsheets based on column values

    Hi,,

    Please provide steps to develop on my sheet.

    Thanks!

  18. #18
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Splitting data into seperate spreadsheets based on column values

    Hi ,

    I just opened your Updated Split Data Based On Key Column By Flabbiness.xlsm‎,it seems like the add-ins tab is attached to my local sheet as well.How to remove it?
    Thanks

  19. #19
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Splitting data into seperate spreadsheets based on column values

    Hi,

    Actually I need to create the tab in ribbon under that tab I need to create some sub tabs.

    Please help.

  20. #20
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Splitting data into seperate spreadsheets based on column values

    Check this code and change in places marked

    Sub ADD_MENU()
    
    Dim cbMainMenuBar As CommandBar
    Dim cbcCutomMenu As CommandBarControl
    
    'reset the commandbar
    Application.CommandBars("Worksheet Menu Bar").Reset
    On Error Resume Next
    
    
    Application.CommandBars("Worksheet Menu Bar").Controls("&Split Data Based On Column Value").Delete
    
    On Error GoTo 0
    
     Set cbMainMenuBar = _
         Application.CommandBars("Worksheet Menu Bar")
         
     Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup)
     
         cbcCutomMenu.Caption = "Process" 
         cbcCutomMenu.OnAction = "unique_values_from_key_column" 'rename this to the macro that your are executing
    End Sub

    Call the add_menu code when your workbook is opened.

    Private Sub Workbook_Open()
    ADD_MENU
    End Sub

  21. #21
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: Splitting data into seperate spreadsheets based on column values

    Reset the command bar.

    'reset the commandbar
    Application.CommandBars("Worksheet Menu Bar").Reset

  22. #22
    Forum Contributor
    Join Date
    08-12-2012
    Location
    mysore
    MS-Off Ver
    Excel 2007
    Posts
    212

    Re: Splitting data into seperate spreadsheets based on column values

    Hi,

    Actually I need to create the tab called data-validate on ribbon not inside add-ins and under that data-validate tab Need to create the tab called split-sheets.
    For this code:
    Sub col_to_sheetz()

    Const cl& = 1
    Dim lr&, lc&, s7, i&
    Dim hdr, q As String, sh As Worksheet

    Application.ScreenUpdating = False
    lr = Cells.Find("*", , , , xlByRows, xlPrevious).Row
    lc = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
    s = 2
    Set ash = ActiveSheet

    With Sheets.Add(after:=ash)
    ash.Cells(1).Resize(lr, lc).Copy .Cells(1)
    hdr = .Cells(1).Resize(, lc)
    .Cells(1).Resize(lr, lc).Sort .Cells(cl), Header:=xlYes
    a = .Cells(cl).Resize(lr + 1)
    For i = 2 To lr
    If a(i, 1) <> a(i + 1, 1) Then
    q = CStr(a(i, 1))
    On Error Resume Next
    Set sh = Sheets(q)
    On Error GoTo 0
    If sh Is Nothing Then
    Sheets.Add(after:=Sheets(Sheets.Count)).Name = q
    .Cells(s, 1).Resize(i - s + 1, lc).Copy Sheets(q).Cells(2, 1)
    s = i + 1
    Sheets(q).Cells(1).Resize(, lc) = hdr
    End If
    End If
    Next i
    Application.DisplayAlerts = False
    .Delete
    Application.DisplayAlerts = True
    End With

    ash.Activate
    Application.ScreenUpdating = True
    End Sub

+ 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] Counting Multiple Values in a column based on critera in seperate column
    By ERoberts in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-11-2013, 01:08 PM
  2. Splitting out Excel data to multiple files based on one column's values
    By machos in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2011, 08:55 AM
  3. Import Data from multiple spreadsheets into seperate spreadsheets worksheet
    By cablecrt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-17-2008, 08:01 AM
  4. Splitting data from sheets into seperate workbooks based on formula?
    By raggajunglist in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-08-2007, 10:45 AM
  5. Replies: 2
    Last Post: 02-08-2005, 04:06 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