如何在由逗号分隔的单元格中创建可能的字符串组合列表

时间:2017-08-28 15:19:28

标签: vba excel-vba combinations excel

让我解释一下我想做的事情。我有一个包含两列的事件列表:事件ID和来宾。在Guests字段中,我收到了以逗号分隔的特定事件的每个参与者的电子邮件。每个活动都有不同数量的参与者。我需要的是列出每个活动的所有可能的客人组合。它看起来像这样:

在一次会议中假设参与者是A,B,C,D

这表示以下唯一的1:1连接

AB
AC
AD
BC
BD
CD

你们中的任何人都知道可以获得此输出的代码吗?

@ Gary的学生谢谢!

这就是我所拥有的,但它无法正常运作。

Excel table

 Sub test1()


Set l1 = Worksheets("Sheet1").Range("A1:AZ10000").Find("Event ID", lookat:=xlPart) 'Location of event id field
Set l2 = Worksheets("Sheet1").Range("A1:AZ10000").Find("Participants123", lookat:=xlPart) 'Location of guests field
Last = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Dim c As Double
Dim l As Double
l = l2.Row + 1



For i = 1 To Last

    last1 = Sheet1.Cells(l, l2.Column).End(xlToRight).Column
    c = Application.Combin(last1 - 3, 2)
    Rows(l + 1 & ":" & l + c - 1).Insert
'Dim N As Long, i As Long, j As Long, K As Long
    N = last1
    K = l
    For b = 4 To N - 1
        For j = b + 1 To N
            Cells(K, 2).Value = Cells(l, b).Value
            Cells(K, 3).Value = Cells(l, j).Value
            K = K + 1
        Next j
    Next b
    l = l + c



Next i

End Sub

! (http://imgur.com/72nW9uR

1 个答案:

答案 0 :(得分:2)

将名称放在 A 列中并运行如下内容:

Sub dural()
Dim N As Long, i As Long, j As Long, K As Long
    N = 4
    K = 1
    For i = 1 To N - 1
        For j = i + 1 To N
            Cells(K, 2).Value = Cells(i, 1).Value & Cells(j, 1).Value
            K = K + 1
        Next j
    Next i
End Sub

enter image description here

这假设您只需要配对,因此不考虑 ABC 。还假设 AB BA 是相同的。