+ Reply to Thread
Results 1 to 3 of 3

Grouping by Categories and SubCategories

Hybrid View

  1. #1
    Registered User
    Join Date
    10-31-2014
    Location
    Albany, New York
    MS-Off Ver
    2010
    Posts
    32

    Post Grouping by Categories and SubCategories

    Dear Experts,
    This is related to my previous post, but with different raw data set and format.
    I need help with writing a macro that will group data under different categories and sub-categories and summarize unique data by using data suffix.

    Attached is the sample spreadsheet.

    Output1 Sheet: From the "Raw Data" sheet, group each data (File ID) under each category (Folder) and sub-category (Sub-Folder)

    Output 2 Sheet:
    ColA: Summarize all File IDs
    ColB: Unique suffix data (value before first dash of the File ID)
    ColC: Value of Concatenated ColB Data, separated by Vertical Bar Pipe


    Thank you!
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    07-14-2017
    Location
    Poland
    MS-Off Ver
    Office 2010
    Posts
    536

    Re: Grouping by Categories and SubCategories

    Hi, perhaps my attempt will be successful.
    Sub Output1_2()
        Dim a(), id, k, kk, v, All_IDs
        Dim d As Object, r As Object
        Dim i As Integer, ii As Integer, rws As Integer
        Dim ms As String, tmp As String
            
        With Sheets("Raw Data")
            lr = .Cells(Rows.Count, "A").End(xlUp).Row
            a = .Range("A2:A" & lr).Value
        End With
        rws = UBound(a)
        Set r = CreateObject("VBScript.RegExp")
        r.IgnoreCase = True
        r.Global = True
        r.Pattern = "[^0-9]+"
        Set d = CreateObject("Scripting.Dictionary")
        With d
            For i = 1 To rws
                ms = Application.Substitute(a(i, 1), "Location:", "")
                If ms Like "Folder*" Then
                    tmp = ms
                    If Not .exists(ms) Then _
                        Set .Item(ms) = CreateObject("Scripting.Dictionary")
                Else
                    If tmp Like "Folder*" And a(i, 1) Like "SubFolder*" Then
                        If r.test(a(i + 1, 1)) Then
                                v = r.Execute(a(i + 1, 1))(0)
                            .Item(tmp).Item(ms) = Application.Substitute(a(i + 1, 1), v, "")
                        End If
                        i = i + 1
                    End If
                End If
            Next i
        End With
        With Sheets("testOut1")
            .UsedRange.ClearContents
            i = 1
            For Each k In d.keys
                .Cells(.Cells(Rows.Count, i).End(3).Row, i) = k
                ii = ii + 2
                For Each kk In d.Item(k).keys
                    .Cells(.Cells(Rows.Count, i).End(3).Row + ii, i) = kk
                    ii = ii - 1
                    id = d.Item(k).Item(kk)
                    All_IDs = IIf(Len(All_IDs) = 0, id, All_IDs & "," & id)
                    id = Split(id, ", ")
                    .Cells(.Cells(Rows.Count, i).End(3).Row + ii, i).Resize(UBound(id) + 1, 1) = _
                                                                                    Application.Transpose(id)
                    ii = 2
                Next
                i = i + 1: ii = 0
            Next
        End With
        v = Split(All_IDs, ",")
        d.RemoveAll
        With Sheets("testOut2")
            .UsedRange.Offset(1, 0).ClearContents
            For i = 0 To UBound(v)
                ms = Trim(Split(v(i), "-")(0))
                d(ms) = Empty
            Next
            .[A2].Resize(UBound(v)) = Application.Transpose(v)
            .[B2].Resize(d.Count) = Application.Transpose(d.keys)
            v = Join(d.keys, " | ")
            .[C2] = Application.Transpose(v)
        End With
        Set d = Nothing
        Set r = Nothing
    End Sub
    Attached Files Attached Files
    Best Regards,
    Maras.

  3. #3
    Registered User
    Join Date
    10-31-2014
    Location
    Albany, New York
    MS-Off Ver
    2010
    Posts
    32

    Re: Grouping by Categories and SubCategories

    @maras_mak! Thank you. That works! Faster and more efficient by miles than the code I initially built. I appreciate your help!

+ 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] Grouping Data by Category and SubCategories and Summarizing unique data
    By mgcarino14 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-05-2019, 10:49 AM
  2. Two level donut chart, categories and subcategories percentage
    By SuperCoog in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 01-31-2019, 05:19 AM
  3. How to organize and filter data with multiple categories and subcategories
    By rooiro in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-01-2018, 12:35 PM
  4. grouping into age categories on excel
    By ttsakok in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-24-2014, 09:45 AM
  5. [SOLVED] Pie Chart not grouping categories together
    By rushdenx1 in forum Excel General
    Replies: 1
    Last Post: 04-03-2012, 08:41 AM
  6. Grouping categories within categories
    By mju4t in forum Excel General
    Replies: 1
    Last Post: 11-03-2010, 02:41 PM
  7. [SOLVED] Switching Subcategories into Categories and Vice Versa
    By conorfinnegan@gmail.com in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 04-17-2006, 10:30 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