简体   繁体   中英

String split and count in Excel

I have the following column K in Excel:

  K           
2 Apps -                                                            
3 Appointed CA - Apps - Assist - Appointed NA - EOD Efficiency
4 Appointed CA -
5 Appointed CA -

I want to split at - and count the number occurrences of the specific words in the string.

I tried the following formula which splits my string and returns everything LEFT to the -

=LEFT( K2, FIND( "-", K2 ) - 2 )

But the ideal output should be:

Apps      Appointed CA       Assist    Appointed NA        EOD Efficiency
1
1           1                 1           1                  1
            1
            1

Based on the above data.

Regards,

Here is a VBA macro that will

  • Generate a unique list of phrases from all of the data
  • Create the "header row" containing the phrases for the output
  • Go through the original data again and generate the counts for each phrase

As written, the macro is case insensitive. To make it case sensitive, one would have change the method of generating the unique list -- using the Dictionary object instead of a collection.

To enter this Macro (Sub), alt-F11 opens the Visual Basic Editor. Ensure your project is highlighted in the Project Explorer window. Then, from the top menu, select Insert/Module and paste the code below into the window that opens. It should be obvious where to make changes to handle variations in where your source data is located, and where you want the results.

To use this Macro (Sub), alt-F8 opens the macro dialog box. Select the macro by name, and RUN .

It will generate results as per your ideal output above


Option Explicit
Option Compare Text
Sub CountPhrases()
    Dim colP As Collection
    Dim wsSrc As Worksheet, wsRes As Worksheet, rRes As Range
    Dim vSrc As Variant, vRes() As Variant
    Dim I As Long, J As Long, K As Long
    Dim V As Variant, S As String

'Set Source and Results worksheets and ranges
Set wsSrc = Worksheets("sheet1")
Set wsRes = Worksheets("sheet2")
    Set rRes = wsRes.Cells(1, 1) 'Results will start in A1 on results sheet

'Get source data and read into array
With wsSrc
    vSrc = .Range("K2", .Cells(.Rows.Count, "K").End(xlUp))
End With

'Collect unique list of phrases
Set colP = New Collection

On Error Resume Next 'duplicates will return an error
For I = 1 To UBound(vSrc, 1)
    V = Split(vSrc(I, 1), "-")
    For J = 0 To UBound(V)
        S = Trim(V(J))
        If S <> "" Then colP.Add S, CStr(S)
    Next J
Next I
On Error GoTo 0

'Dimension results array
'Row 0 will be for the column headers
ReDim vRes(0 To UBound(vSrc, 1), 1 To colP.Count)

'Populate first row of results array
For J = 1 To colP.Count
    vRes(0, J) = colP(J)
Next J

'Count the phrases
For I = 1 To UBound(vSrc, 1)
    V = Split(vSrc(I, 1), "-")
    For J = 0 To UBound(V)
        S = Trim(V(J))
        If S <> "" Then
            For K = 1 To UBound(vRes, 2)
                If S = vRes(0, K) Then _
                    vRes(I, K) = vRes(I, K) + 1
            Next K
        End If
    Next J
Next I

'write results
Set rRes = rRes.Resize(UBound(vRes, 1) + 1, UBound(vRes, 2))
With rRes
    .EntireColumn.Clear
    .Value = vRes
    .EntireColumn.AutoFit
End With

End Sub

Assuming result range starts in column L:

L2: =IF(FIND("Apps", K2, 1) <> 0, 1, "")
M2: =IF(FIND("Appointed CA", K2, 1) <> 0, 1, "")

etc.

Autofill downwards.

EDIT:

Assuming all possible string combinations we're looking for are known ahead of time, the following should work. If the possible string combinations are not known, I would recommend building a UDF to sort it all out.

Anyway, assuming the strings are known, following the same principle as above:

L2: =IF(FIND("Apps", K2, 1) <> 0, (LEN(K2) - LEN(SUBSTITUTE(K2, "Apps", "")) / LEN(K2)), "")
M2: =IF(FIND("Appointed CA", K2, 1) <> 0, (LEN(K2) - LEN(SUBSTITUTE(K2, "Appointed CA", "")) / LEN(K2)), "")

Increase for as many strings as you like, autofill downwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM