【问题标题】:VBA/Excel - Count unique words in columns with multiple words in each cellVBA/Excel - 计算每个单元格中包含多个单词的列中的唯一单词
【发布时间】:2021-10-27 09:50:05
【问题描述】:

我正在使用以下数据集。对于每家公司,我想了解他们订购了多少种不同的产品。

例如:公司“AAA”订购了 6 种不同的产品(产品 1、2、3、4、5、7)。

不确定,如果我们需要在每一列中拆分单词,然后在循环中一个一个地计数,或者有没有更快的方法? 我这里得用VBA,我的数据集10万多。

【问题讨论】:

  • 你只需要数一数吗?也没有显示他订购了哪些产品
  • 是的,只有唯一计数
  • 您的澄清答案稍晚一些。与此同时,我发布了一段代码,返回唯一的客户名称,后面跟着订购的产品数量和以下列中的每个订购的产品名称。我(仅)认为它会更有用。请尝试一下。否则,代码可以更简单......它在另一张纸中返回。如果您需要,它可以很容易地调整为在同一张纸中返回。

标签: excel vba unique


【解决方案1】:

假设A1:C? 中的数据,您也许可以将一些东西拼凑在一起:

Sub Test()

Dim arr As Variant
Dim lr As Long, x As Long, y As Long
Dim dict1 As Object: Set dict1 = CreateObject("Scripting.Dictionary")
Dim dict2 As Object: Set dict2 = CreateObject("Scripting.Dictionary")

'Get initial array (NOTE: implicit reference to the active worksheet)
lr = Cells(Rows.Count, "A").End(xlUp).Row
arr = Range("A2:C" & lr)

'Loop through array and fill dictionary
For x = LBound(arr) To UBound(arr)
    dict1(arr(x, 1)) = dict1(arr(x, 1)) & "," & arr(x, 3)
Next

'Loop through dictionary and count unique items
For y = 0 To dict1.Count - 1
    For Each el In Split(dict1.Items()(y), ",")
        dict2(el) = 1
    Next
    dict1(dict1.keys()(y)) = dict2.Count - 1
    dict2.RemoveAll
    
    'Check the result
    Debug.Print dict1.keys()(y) & "-" & dict1.Items()(y)
Next

End sub

【讨论】:

  • 非常优雅的解决方案。它也应该很快。
  • 这很棒 - 非常感谢
【解决方案2】:

请测试下一个代码。它将返回(在下一张表中的上述代码中,但它可以在任何一张表中返回)唯一客户,然后是总产品数,在接下来的列中是订购的产品:

Sub ProductsPerClient()
    Dim sh As Worksheet, sh1 As Worksheet, lastR As Long, arr, arrSpl, arrFin, colMax As Long
    Dim i As Long, j As Long, dict As Object
    
    Set sh = ActiveSheet
    Set sh1 = sh.Next 'use here the sheet you need
    lastR = sh.Range("A" & sh.rows.count).End(xlUp).row
    arr = sh.Range("A2:C" & lastR).value
    Set dict = CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(arr)
        arrSpl = Split(Trim(arr(i, 3)), ",")
        If Not dict.Exists(arr(i, 1)) Then
            dict.Add arr(i, 1), Join(arrSpl, "|")
            If UBound(arrSpl) + 1 > colMax Then colMax = UBound(arrSpl) + 1
        Else
            dict(arr(i, 1)) = dict(arr(i, 1)) & "|" & Join(arrSpl, "|")
            If UBound(Split(dict(arr(i, 1)), "|")) + 1 > colMax Then colMax = UBound(Split(dict(arr(i, 1)), "|")) + 1
        End If
    Next i
    ReDim arrFin(1 To dict.count, 1 To colMax + 2)

    For i = 0 To dict.count - 1
        arrFin(i + 1, 1) = dict.Keys()(i)
        arrSpl = Split(dict.items()(i), "|")
        arrFin(i + 1, 2) = UBound(arrSpl) + 1
        For j = 0 To UBound(arrSpl)
            arrFin(i + 1, j + 3) = arrSpl(j)
        Next j
    Next i
    'drop the final array content:
    sh1.Range("A2").Resize(UBound(arrFin), UBound(arrFin, 2)).value = arrFin
 End Sub

【讨论】:

    【解决方案3】:

    这个答案可能看起来很傻,但是当您用逗号分隔不同的产品时,为什么不简单地计算逗号的数量并加 1,例如:

    =SEARCH(",",C2,1)+1
    

    在帮助列中添加此信息后,您可以使用 Excel 的基本 Subtotals 功能来查找每个客户的总和。

    【讨论】:

    • 我认为这是不同的,在您的情况下,我们将计算所有产品,我们想要独特产品的数量。例如。在你的情况下 Product_7 将被复制
    • @Lohengrin:谢谢你的评论。当我写下我的答案时,还不清楚是否需要唯一的条目。尽管如此,出于参考原因,我想保留我的答案(您永远不知道一个人在没有独特条件的情况下也有类似的问题,这个答案可以帮助他)。
    猜你喜欢
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2021-06-18
    • 2018-04-13
    • 1970-01-01
    • 2016-03-21
    • 1970-01-01
    相关资源
    最近更新 更多