excel - 列出所有可能的组合而不重复,VBA

标签 excel vba unique combinations

我有一个代码现在可以使用并列出 6 位数字。但它们是重复的。但是这些数字是重复的。我需要唯一的非重复 6 位数字。 我现在有这种结果。1 1 1 3 4 6 但我需要不同且不重复的结果。谢谢你帮助我。

Sub AllCombinations()
Dim nums(): nums = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
Dim arValues(999999, 5)
Dim n1 As Integer, n2 As Integer, n3 As Integer, n4 As Integer, n5 As Integer, n6 As Integer, x As Long

For n1 = 0 To UBound(nums)
    For n2 = 0 To UBound(nums)
        For n3 = 0 To UBound(nums)
            For n4 = 0 To UBound(nums)
                For n5 = 0 To UBound(nums)
                    For n6 = 0 To UBound(nums)
                    arValues(x, 0) = nums(n1)
                    arValues(x, 1) = nums(n2)
                    arValues(x, 2) = nums(n3)
                    arValues(x, 3) = nums(n4)
                    arValues(x, 4) = nums(n5)
                    arValues(x, 5) = nums(n6)
                    x = x + 1
                Next
            Next
        Next
       Next
   Next
  Next
      Range("A1").Resize(1000000, 6).Value2 = arValues

 End Sub

最佳答案

就目前而言,如果您想找到不同长度的组合或具有不同 Ubound 的数组的组合,您将不得不更改您的代码。这会变得非常乏味并且容易出错。这是一个更通用的解决方案,适用于任何类型、任何大小和任何输出长度的数组。

Sub CombosNoRep(ByRef v() As Variant, r As Long)
Dim i As Long, k As Long, z() As Variant, comboMatrix() As Variant
Dim numRows As Long, numIter As Long, n As Long, count As Long

    count = 1
    n = UBound(v)
    numRows = nChooseK(n, r)

    ReDim z(1 To r)
    ReDim comboMatrix(1 To numRows, 1 To r)
    For i = 1 To r: z(i) = i: Next i

    Do While (count <= numRows)
        numIter = n - z(r) + 1
        For i = 1 To numIter
            For k = 1 To r: comboMatrix(count, k) = v(z(k)): Next k
            count = count + 1
            z(r) = z(r) + 1
        Next i

        For i = r - 1 To 1 Step -1
            If Not (z(i) = (n - r + i)) Then
                z(i) = z(i) + 1
                For k = (i + 1) To r: z(k) = z(k - 1) + 1: Next k
                Exit For
            End If
        Next i
    Loop

    Range("A1").Resize(numRows, r).Value2 = comboMatrix
End Sub

Function nChooseK(n As Long, k As Long) As Long
''returns the number of k-combinations from a set
''of n elements. Mathematically speaking, we have: n!/(k!*(n-k)!)
Dim temp As Double, i As Long
    temp = 1
    For i = 1 To k: temp = temp * (n - k + i) / i: Next i
    nChooseK = CLng(temp)
End Function

调用它我们有:

Sub Test()
Dim myArray(1 To 9) As Variant, i As Long
    For i = 1 To 9: myArray(i) = i: Next i
    Call CombosNoRep(myArray, 6)
End Sub

这会快速输出所有 84 种独特的组合。

1-9 choose 4

让我们在包含字符串的数组上尝试一下。

Sub Test()
Dim myArray() As Variant, i As Long
    '' Added blank "" as CombosNoRep is expecting base 1 array
    myArray = Array("", "Canada", "England", "Laos", "Ethiopia", "Burma", "Latvia", "Serbia", "Chile", "France", "Tonga")
    Call CombosNoRep(myArray, 4)
End Sub

这里我们有国家数组的所有 4 元组(210 个独特的组合)。

myCountries

关于excel - 列出所有可能的组合而不重复,VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48150054/

相关文章:

ms-access - VBA连接到 protected 数据库,错误处理

excel - 对 Excel 文件运行批处理并编辑 VBA 代码

arrays - 为什么 Excel.Range.Group 的 Periods 参数需要一个数组?

excel - 通过比较两个不同的关键字来填充颜色

excel - 使用 Excel VBA 宏查找并替换 Word 中的页脚文本

sql - 如何在分区的 MySQL 数据库中的字段上添加唯一索引?

r - 使用 R 将 data.frame 转换为基于 dataframe 的某一列的字符列表

java - 如何在每次运行我的应用程序时生成一个唯一的随机数?

vba - 将信息从一个选项卡拉到另一个选项卡

vba - 运行 VBA 脚本导致 Excel 停止响应