简体   繁体   English

在Excel中进行字符串拆分和计数

[英]String split and count in Excel

I have the following column K in Excel: 我在Excel中有以下K栏:

  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-

=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 这是一个VBA宏

  • 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. 要输入此宏(子), alt-F11打开Visual Basic编辑器。 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. 要使用此宏(子),请按alt-F8打开宏对话框。 Select the macro by name, and RUN . 通过名称选择宏,然后选择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: 假设结果范围从L列开始:

L2: =IF(FIND("Apps", K2, 1) <> 0, 1, "") L2: =IF(FIND("Apps", K2, 1) <> 0, 1, "")
M2: =IF(FIND("Appointed CA", 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. 如果不知道可能的字符串组合,我建议您构建一个UDF以将其全部整理出来。

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)), "") 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)), "") 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. 增加任意多的字符串,向下自动填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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