Vba 同一行上两列中每种可能的值组合的出现频率

Vba 同一行上两列中每种可能的值组合的出现频率,vba,excel,frequency,Vba,Excel,Frequency,我有一个数据集,产品1在a列,产品2在B列 我想建立一个新表,统计产品1和产品2的每种可能组合出现的行数。(最好是不考虑它们发生的顺序,但如果需要,我可以在之后清理) 我可以手动构建它,但是我正在处理数百种可能的组合,并且希望使用宏或任何其他建议来自动化该过程 原始数据示例: Product 1 Product 2 Cheese Apple Crackers Sausage Cheese Sausage Crackers Sausage App

我有一个数据集,产品1在a列,产品2在B列

我想建立一个新表,统计产品1和产品2的每种可能组合出现的行数。(最好是不考虑它们发生的顺序,但如果需要,我可以在之后清理)

我可以手动构建它,但是我正在处理数百种可能的组合,并且希望使用宏或任何其他建议来自动化该过程

原始数据示例:

Product 1   Product 2
Cheese          Apple
Crackers    Sausage
Cheese          Sausage
Crackers    Sausage
Apple           Crackers
Apple           Cheese
Cheese          Apple
Cherry          Apple
新汇总表示例:

Combo               | Count of Combo Occurrences
Cheese and Apple    | 3
Cheese and Sausage  | 1
Cherry and Apple    | 1
Crackers and Sausage| 2
Apple and Crackers  | 1

提前感谢

以防万一一些可怜的灵魂在VBA中需要这个:

Option Explicit
Sub ComboOccurences()

    ' Remember to check Microsoft Scripting Runtime in References!
    Dim dict As Scripting.Dictionary
    Dim i As Integer, r As Integer, LastRow As Integer
    Dim ColAB As String, ColBA As String

    Set dict = New Scripting.Dictionary
    LastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row

    For i = 2 To LastRow
        ColAB = Range("A" & i).Value & " and " & Range("B" & i).Value
        ColBA = Range("B" & i).Value & " and " & Range("A" & i).Value
        If Not dict.Exists(ColAB) And Not dict.Exists(ColBA) Then
            dict.Add (ColAB), 1
        ElseIf dict.Exists(ColAB) Then
            dict(ColAB) = dict(ColAB) + 1
        ElseIf dict.Exists(ColBA) Then
            dict(ColBA) = dict(ColBA) + 1
        End If
    Next

    r = 2
    For i = 0 To dict.Count - 1
        Range("D" & r).Value = dict.Keys(i)
        Range("E" & r).Value = dict.Items(i)
        r = r + 1
    Next

End Sub
结果:


希望这能帮助别人

以防某个可怜的灵魂在VBA中需要这个:

Option Explicit
Sub ComboOccurences()

    ' Remember to check Microsoft Scripting Runtime in References!
    Dim dict As Scripting.Dictionary
    Dim i As Integer, r As Integer, LastRow As Integer
    Dim ColAB As String, ColBA As String

    Set dict = New Scripting.Dictionary
    LastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row

    For i = 2 To LastRow
        ColAB = Range("A" & i).Value & " and " & Range("B" & i).Value
        ColBA = Range("B" & i).Value & " and " & Range("A" & i).Value
        If Not dict.Exists(ColAB) And Not dict.Exists(ColBA) Then
            dict.Add (ColAB), 1
        ElseIf dict.Exists(ColAB) Then
            dict(ColAB) = dict(ColAB) + 1
        ElseIf dict.Exists(ColBA) Then
            dict(ColBA) = dict(ColBA) + 1
        End If
    Next

    r = 2
    For i = 0 To dict.Count - 1
        Range("D" & r).Value = dict.Keys(i)
        Range("E" & r).Value = dict.Items(i)
        r = r + 1
    Next

End Sub
结果:


希望这能帮助别人

聚会迟到了,但你的问题似乎很有趣。对于kicks,我决定添加一层额外的复杂性,将其写入任何大小范围,并将结果输出到指定范围(或表)

子测试()
呼叫计数唯一组合(范围(“A2:D7”)、范围(“F2”))
端接头
专用子计数唯一组合(ByVal SourceRange作为范围,ByVal DestinationRange作为范围)
暗淡的Orovindex长
与林德克斯一样长
Dim oRow作为新系列
对于oRowIndex=0到SourceRange.Rows.Count-1
oValue=“”
设置oRow=无
'对当前行排序(输出为字符串)
对于oColIndex=1到SourceRange.Columns.Count
oRow.添加SourceRange(OROVINDEX+1,oColIndex).Value
下一个
oValue=SortCollection(oRow)
'如果so+1,则查看已排序行是否已存在
暗色和长色一样
作为布尔值的深度
oFound=False
对于oDestRowIndex=1到DestinationRange.Rows.Count
如果DestinationRange(oDestRowIndex,1).Value=oValue,则
DestinationRange(oDestRowIndex,2).Value=CInt(DestinationRange(oDestRowIndex,2).Value)+1
oFound=真
退出
如果结束
下一个
'如果排序行不存在,请添加它
如果不是很深的话
DestinationRange(DestinationRange.Rows.Count,1)=oValue
DestinationRange(DestinationRange.Rows.Count,1)。偏移量(0,1)=1
设置DestinationRange=DestinationRange.Resize(DestinationRange.Rows.Count+1,1)
如果结束
下一个
端接头
私有函数SortCollection(ByVal oCollection作为集合)作为字符串
牛一样长,oY一样长
将值设置为字符串
对于oX=1到oCollection.Count-1
对于oY=oX+1到oCollection.Count
如果oCollection(oX)>oCollection(oY),则
oTempValue=oCollection(oY)
o收集。移除(oY)
oCollection。添加oTempValue、oTempValue和oX
如果结束
下一个
下一个
对于oX=1到oCollection.Count
如果oCollection.Item(oX)为“”,则
SortCollection=SortCollection&oCollection.Item(oX)和“&”
如果结束
下一个
SortCollection=左侧(SortCollection,Len(SortCollection)-3)
端函数

晚会迟到了,但你的问题似乎很有趣。对于kicks,我决定添加一层额外的复杂性,将其写入任何大小范围,并将结果输出到指定范围(或表)

子测试()
呼叫计数唯一组合(范围(“A2:D7”)、范围(“F2”))
端接头
专用子计数唯一组合(ByVal SourceRange作为范围,ByVal DestinationRange作为范围)
暗淡的Orovindex长
与林德克斯一样长
Dim oRow作为新系列
对于oRowIndex=0到SourceRange.Rows.Count-1
oValue=“”
设置oRow=无
'对当前行排序(输出为字符串)
对于oColIndex=1到SourceRange.Columns.Count
oRow.添加SourceRange(OROVINDEX+1,oColIndex).Value
下一个
oValue=SortCollection(oRow)
'如果so+1,则查看已排序行是否已存在
暗色和长色一样
作为布尔值的深度
oFound=False
对于oDestRowIndex=1到DestinationRange.Rows.Count
如果DestinationRange(oDestRowIndex,1).Value=oValue,则
DestinationRange(oDestRowIndex,2).Value=CInt(DestinationRange(oDestRowIndex,2).Value)+1
oFound=真
退出
如果结束
下一个
'如果排序行不存在,请添加它
如果不是很深的话
DestinationRange(DestinationRange.Rows.Count,1)=oValue
DestinationRange(DestinationRange.Rows.Count,1)。偏移量(0,1)=1
设置DestinationRange=DestinationRange.Resize(DestinationRange.Rows.Count+1,1)
如果结束
下一个
端接头
私有函数SortCollection(ByVal oCollection作为集合)作为字符串
牛一样长,oY一样长
将值设置为字符串
对于oX=1到oCollection.Count-1
对于oY=oX+1到oCollection.Count
如果oCollection(oX)>oCollection(oY),则
oTempValue=oCollection(oY)
o收集。移除(oY)
oCollection。添加oTempValue、oTempValue和oX
如果结束
下一个
下一个
对于oX=1到oCollection.Count
如果oCollection.Item(oX)为“”,则
SortCollection=SortCollection&oCollection.Item(oX)和“&”
如果结束
下一个
SortCollection=左侧(SortCollection,Len(SortCollection)-3)
端函数

@user3088527这是个大问题。通常,没有代码的问题和由代码导致的特定错误会被关闭并被否决。你有没有试过的代码?如果是这样的话,你们能在你们原来的帖子里用吗?我有点希望它不会被关闭,我想尝试一下,虽然我的解决方案可能会很臃肿,可能会重复:用li