excelvbaexcel-formulaexcel-2010

Split text into smaller one and group by each ID


I have an Excel table like this

enter image description here


Now from the second column, I want to split them into small substring and group them in each ID group, like this

enter image description here

Please let me know how I can do that programmatically.

Since I'm new from Excel, I have tried this but it does not work: https://support.microsoft.com/en-us/office/split-text-into-different-columns-with-the-convert-text-to-columns-wizard-30b14928-5550-41f5-97ca-7a3e9c363ed7


Solution

  • Option Explicit
    
    Sub Demo()
        Dim i As Long, j As Long
        Dim arrData, arrRes, aTxt, sTxt As String
        Dim idCol As New Collection, ruleCol As New Collection
        arrData = ActiveSheet.Range("A1").CurrentRegion.Value
        For i = LBound(arrData) To UBound(arrData)
            aTxt = Split(arrData(i, 2), ">"): sTxt = ""
            For j = LBound(aTxt) To UBound(aTxt)
                idCol.Add arrData(i, 1)
                sTxt = sTxt & ">" & aTxt(j)
                ruleCol.Add Mid(sTxt, 2)
            Next j
        Next i
        ReDim arrRes(1 To idCol.Count, 1)
        For i = 1 To idCol.Count
            arrRes(i, 0) = idCol(i)
            arrRes(i, 1) = ruleCol(i)
        Next
        Sheets.Add
        Range("A1").Resize(idCol.Count, 2).Value = arrRes
    End Sub