簡體   English   中英

VBA/Excel - 計算每個單元格中有多個單詞的列中的唯一單詞

[英]VBA/Excel - Count unique words in columns with multiple words in each cell

我正在使用以下數據集。 對於每家公司,我想了解他們訂購了多少種不同的產品。

例如:公司“AAA”訂購了 6 種不同的產品(產品 1、2、3、4、5、7)。

例子

不確定,如果我們需要在每列中拆分單詞,然后在循環中一一計數,或者有什么更快的方法? 這里必須使用VBA,我的數據集超過100k。

假設數據在A1:C? ,您可以使用拼湊一些東西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

這個答案可能看起來很愚蠢,但是當您用逗號分隔不同的產品時,為什么不簡單地計算逗號的數量並加 1,例如:

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

一旦您在輔助列中找到了它,您就可以使用 Excel 的基本Subtotals功能來查找每個客戶的總和。

請測試下一個代碼。 它將返回(在下一個工作表中的上述代碼中,但它可以在任何工作表中返回)唯一客戶,然后是產品總數,在下一列中是訂購的產品:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM