Unique count of words from text string

BigOleNewb

I have a dataset that is multiple strings and I want a unique count of the occurrences so I can review and refine my datasets. I've been unable to do this using formulas so went over to VBA, but hit a roadblock as I'm an amateur.

My data looks like this...

enter image description here

I want it to return this...

enter image description here

I've tried parsing it with text to columns, but in large datasets I have 60 columns with 100s of hits in my string. Therefore, transposing it then trying to get a count of uniques would be daunting.

Therefore, I was hoping VBA would help, but I can only seem to get a function and not with a Sub and Function to transpose then count. Something like below...

Sub Main()
    Dim filename As String
    Dim WorksheetName As String
    Dim CellRange As String
    
    Sheets.Add.Name = "ParsedOutput"

'==============================================================
' CHANGE THESE VALUES FOR YOUR SHEET   
WorksheetName =   
CellRange =    
'==============================================================
   
    ' Get range
    Dim Range
    Set Range = ThisWorkbook.Worksheets(WorksheetName).Range(CellRange)

    ' Copy range to avoid overwrite
    Range.Copy _
        Destination:=ThisWorkbook.Worksheets("ParsedOutput").Range("A1")
        
    ' Get copied exclusions
    Dim Copy
    Set Copy = ThisWorkbook.Worksheets("ParsedOutput").Range("A:A")
    
    ' Parse and overwrite
    Copy.TextToColumns _
        Destination:=Range("A:A"), _
        DataType:=xlDelimited, _
        ConsecutiveDelimiter:=True, _
        Comma:=True

End Sub

Option Explicit

Public Function Counter(InputRange As Range) As String

Dim CellValue As Variant, UniqueValues As New Collection

Application.Volatile

'For error Handling On Error Resume Next

'Looping through all the cell in the defined range For Each CellValue In InputRange
    UniqueValues.Add CellValue, CStr(CellValue)  ' add the unique item Next

'Returning the count of number of unique values CountUniqueValues = UniqueValues.Count

End Function
Ricardo Diaz

The following is a rough approach, and is open to tons of improvements, but should get you started.

Read the comments and adjust the code to fit your needs.

Option Explicit


Public Sub CountWordsInColumn()
    
    ' Adjust to set the sheet holding the data
    Dim sourceSheet As Worksheet
    Set sourceSheet = ThisWorkbook.Worksheets("DataSet")
    
    ' Adjust the column and row that contains the hits
    Dim hitsColumn As String
    Dim hitsStartRow As Long
    Dim lastRow As Long
    hitsColumn = "C"
    hitsStartRow = 2
    lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, hitsColumn).End(xlUp).Row
    
    ' Adjust the column that contains the hits
    Dim sourceRange As Range
    Set sourceRange = sourceSheet.Range(hitsColumn & hitsStartRow & ":" & hitsColumn & lastRow)
    
    ' Add values in each cell split by ,
    Dim evalCell As Range
    Dim splitValues As Variant
    Dim counter As Long
    ReDim splitValues(lastRow - hitsStartRow)
    For Each evalCell In sourceRange
    
        splitValues(counter) = Split(evalCell.Value, ",")
        
        counter = counter + 1
        
    Next evalCell
    
    ' Get all values into an array
    Dim allValues As Variant
    allValues = AddValuesToArray(splitValues)
    
    ' Get unique values into an array
    Dim uniqueValues As Variant
    uniqueValues = GetUniqueValues(allValues)
    
    ' Count duplicated values from unique array
    Dim outputData As Variant
    outputData = CountValuesInArray(uniqueValues, allValues)
    
    ' Add new sheet
    Dim outputSheet As Worksheet
    Set outputSheet = ThisWorkbook.Sheets.Add
    PrintArrayToSheet outputSheet, outputData

End Sub

Private Function AddValuesToArray(ByVal myArray As Variant) As Variant

    Dim counter As Long
    Dim tempArray As Variant
    Dim tempCounter As Long
    Dim tempArrayCounter As Long
    
    ReDim tempArray(0)
    
    For counter = 0 To UBound(myArray)
        
        For tempCounter = 0 To UBound(myArray(counter))
            
            tempArray(tempArrayCounter) = myArray(counter)(tempCounter)
            
            tempArrayCounter = tempArrayCounter + 1
            
            ReDim Preserve tempArray(tempArrayCounter)
        
        Next tempCounter
    
    Next counter
    
    ReDim Preserve tempArray(tempArrayCounter - 1)
    
    AddValuesToArray = tempArray

End Function

Private Function GetUniqueValues(ByVal tempArray As Variant) As Variant
    Dim tempCol As Collection
    Set tempCol = New Collection
    
    On Error Resume Next
    Dim tempItem As Variant
    For Each tempItem In tempArray
        tempCol.Add tempItem, CStr(tempItem)
    Next
    On Error GoTo 0
    
    Dim uniqueArray As Variant
    Dim counter As Long
    ReDim uniqueArray(tempCol.Count - 1)
    For Each tempItem In tempCol
        uniqueArray(counter) = tempCol.Item(counter + 1)
        counter = counter + 1
    Next tempItem
    GetUniqueValues = uniqueArray
    
End Function

Function CountValuesInArray(ByVal uniqueArray As Variant, ByVal allValues As Variant) As Variant
    
    Dim uniqueCounter As Long
    Dim allValuesCounter As Long
    Dim ocurrCounter As Long
    Dim outputData As Variant
    
    ReDim outputData(UBound(uniqueArray))
    
    For uniqueCounter = 0 To UBound(uniqueArray)
    
        For allValuesCounter = 0 To UBound(allValues)
        
            If uniqueArray(uniqueCounter) = allValues(allValuesCounter) Then ocurrCounter = ocurrCounter + 1
        
        Next allValuesCounter
        
        ' This is the output
        Debug.Print uniqueArray(uniqueCounter), ocurrCounter
        outputData(uniqueCounter) = Array(uniqueArray(uniqueCounter), ocurrCounter)
        
        ocurrCounter = 0
    
    Next uniqueCounter
    
    CountValuesInArray = outputData
    
End Function

Private Sub PrintArrayToSheet(ByVal outputSheet As Worksheet, ByVal outputArray As Variant)

    Dim counter As Long
    
    For counter = 0 To UBound(outputArray)
    
        outputSheet.Cells(counter + 1, 1).Value = outputArray(counter)(0)
        outputSheet.Cells(counter + 1, 2).Value = outputArray(counter)(1)
    
    Next counter
End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Count the duplicate words in a text mysql

From Dev

Java program to count lines, char, and words from a text file

From Dev

How do I count the number of words in a text (string)?

From Dev

Count of unique characters in string

From Dev

count unique words in string

From Dev

Count of unique words from one column of a file in shell

From Dev

How can you use Python to count the unique words (without special characters/ cases interfering) in a text document

From Dev

Getting a count of unique strings from a List<string[]> into a dictionary

From Dev

Java program to count lines, words, and chars from a text given file

From Dev

Count unique words in a long long string

From Dev

Count number of words in a string

From Dev

match and count frequencies of words exactly from a string in R

From Dev

Unique values with frequency count from List<List<String>>

From Dev

count words unique in array JavaScript

From Dev

Removing specific words from a text string?

From Dev

Count words in a string

From Dev

How to count different words from a text on C?

From Dev

How do I count the number of words in a text (string)?

From Dev

Count of unique characters in string

From Dev

count unique words in string

From Dev

How to separate unique characters from several words in a "indic" text file?

From Dev

Count specific words from text file - Java

From Dev

How do get the count of unique words from a directory of text files in PHP?

From Dev

Need unique count of text from multiple columns by category

From Dev

Words count in a string

From Dev

Remove unique multiple words from string

From Dev

How to print unique words from an inputted string

From Dev

how to generate random text from string of words

From Dev

How to count unique words from a text file after a specific string in every line?

Related Related

  1. 1

    Count the duplicate words in a text mysql

  2. 2

    Java program to count lines, char, and words from a text file

  3. 3

    How do I count the number of words in a text (string)?

  4. 4

    Count of unique characters in string

  5. 5

    count unique words in string

  6. 6

    Count of unique words from one column of a file in shell

  7. 7

    How can you use Python to count the unique words (without special characters/ cases interfering) in a text document

  8. 8

    Getting a count of unique strings from a List<string[]> into a dictionary

  9. 9

    Java program to count lines, words, and chars from a text given file

  10. 10

    Count unique words in a long long string

  11. 11

    Count number of words in a string

  12. 12

    match and count frequencies of words exactly from a string in R

  13. 13

    Unique values with frequency count from List<List<String>>

  14. 14

    count words unique in array JavaScript

  15. 15

    Removing specific words from a text string?

  16. 16

    Count words in a string

  17. 17

    How to count different words from a text on C?

  18. 18

    How do I count the number of words in a text (string)?

  19. 19

    Count of unique characters in string

  20. 20

    count unique words in string

  21. 21

    How to separate unique characters from several words in a "indic" text file?

  22. 22

    Count specific words from text file - Java

  23. 23

    How do get the count of unique words from a directory of text files in PHP?

  24. 24

    Need unique count of text from multiple columns by category

  25. 25

    Words count in a string

  26. 26

    Remove unique multiple words from string

  27. 27

    How to print unique words from an inputted string

  28. 28

    how to generate random text from string of words

  29. 29

    How to count unique words from a text file after a specific string in every line?

HotTag

Archive