Excel - All unique words in a range

后端 未结 2 1338
遇见更好的自我
遇见更好的自我 2021-01-14 15:18

Perhaps I\'m trying to do too much, but I have a column filled with text. Each cell has an arbitrary number of words, so for example:

     |         A                


        
2条回答
  •  自闭症患者
    2021-01-14 15:23

    This option uses 1 loop instead of 3, I like to use a dictionary instead or Collection.

    Sub Sample()
    Dim varValues As Variant
    Dim strAllValues As String
    Dim i As Long
    Dim d As Object
    
    'Create empty Dictionary
    Set d = CreateObject("Scripting.Dictionary")
    
    'Create String With all possible Values
    strAllValues = Join(Application.Transpose(Range("A1", Range("A" & Rows.Count).End(xlUp))), " ")
    
    'Split All Values by space into array
    varValues = Split(strAllValues, " ")
    
    'Fill dictionary with all values (this filters out duplicates)
    For i = LBound(varValues) To UBound(varValues)
        d(varValues(i)) = 1
    Next i
    
    'Write All The values back to your worksheet
    Range("B1:B" & d.Count) = Application.Transpose(d.Keys)
    End Sub
    

提交回复
热议问题