Search code examples
excelvbasplitgroup

Split and group values in excel


Hi I have a column of values which has different suffix after a dot, i need it to group it based on the value after dot. Example i need to split all values that end with .pdf into one column, values with.xls as another column, etc,,, enter image description here

how to do this is my doubt.


Solution

  • you can use nested dictionaries

    Sub test()
    
        With New Scripting.Dictionary
            Dim cel As Range
                For Each cel In Range("A1").CurrentRegion
                    If Not .Exists(Split(cel.Value, ".")(1)) Then .Add Split(cel.Value, ".")(1), New Scripting.Dictionary
                    .Item(Split(cel.Value, ".")(1)).Add cel.Value, 1
                Next
            
                Dim iK As Long
                    For iK = 0 To .Count - 1
                        Range("C1").Offset(, iK).Resize(.Items(iK).Count).Value = Application.Transpose(.Items(iK).Keys)
                    Next
        End With
        
    End Sub
    

    just add reference to "Microsoft Scripting Runtime" library

    enter image description here enter image description here