所以我已经有了排列列表,但我想把它转换为组合。所以我有一个名单“john”“mike”“tom”的名单,这些已经被转换为两列排列“john mike”“john tom”“mike john”“mike tom”“tom john”“ tom mike“,那么我将如何删除冗余排列以将其转换为组合,还是更容易制作新的宏来生成组合?
所以要清楚,我正在寻找的新名单将是“john mike”“john tom”“tom mike”
以下是昨天给我的排列的宏(source)。
Sub Permutation()
Dim NameList As Variant, NameVal As Variant, NameVal2 As Variant
Dim Iter As Long
NameList = Sheet3.Range("A1:A108").Value
Iter = 1
For Each NameVal In NameList
For Each NameVal2 In NameList
If NameVal2 <> NameVal Then
Range("C" & Iter).Value = NameVal
Range("D" & Iter).Value = NameVal2
Iter = Iter + 1
End If
Next NameVal2
Next NameVal
End Sub
答案 0 :(得分:2)
试试这个:
Sub NoRepetition()
Dim NameList As Variant, NameVal As Variant, NameVal2 As Variant
Dim Iter As Long, OutputList As Range, NotYet As Boolean
NameList = Range("A1:A5").Value
Iter = 1
For Each NameVal In NameList
For Each NameVal2 In NameList
LRow = Range("C" & Rows.Count).End(xlUp).Row
Set OutputList = Range("C1:C" & LRow)
NotYet = (Application.CountIf(OutputList, NameVal2) = 0)
If NameVal2 <> NameVal And NotYet Then
Range("C" & Iter).Value = NameVal
Range("D" & Iter).Value = NameVal2
Iter = Iter + 1
End If
Next NameVal2
Next NameVal
End Sub
附加概念很简单:只需添加一个检查以查看名称是否已存在于第一列中。如果是,请跳过该组合。如果没有,请将其放入。
<强>截图:强>
如果有帮助,请告诉我们。