从1列多行VBA中仅获取具有数据的唯一组合

从1列多行VBA中仅获取具有数据的唯一组合,vba,excel,combinations,permutation,Vba,Excel,Combinations,Permutation,我知道如何创建所有可能的组合,但我很难获得唯一的组合 在我的示例中,A3:A7 我的目标是获得数据的所有独特组合,而不是重复的组合 这是我现在掌握的代码 Private Sub CommandButton1_Click() For i = 3 To 7 Cells(i, 1) = i - 2 Next i Cells(1, 3) = "number" Cells(3, 3) = "combinations" For i = 3 To 7 For Z = 3 To 6 last = Cell

我知道如何创建所有可能的组合,但我很难获得唯一的组合

在我的示例中,
A3:A7

我的目标是获得数据的所有独特组合,而不是重复的组合

这是我现在掌握的代码

Private Sub CommandButton1_Click()

For i = 3 To 7
Cells(i, 1) = i - 2
Next i

Cells(1, 3) = "number"
Cells(3, 3) = "combinations"

For i = 3 To 7
For Z = 3 To 6
last = Cells(3, Columns.Count).End(xlToLeft).Column
Range(Cells(3, 1), Cells(7, 1)).Copy Destination:=Range(Cells(3, last + 1), Cells(7, last + 1))
temp = Cells(i, 1)
Cells(i, last + 1) = Cells(Z + 1, 1)
Cells(Z + 1, last + 1) = temp
Next Z
Next i

End Sub
这里可以看到excel文件中发生了什么

如果想法是创建一些随机范围并查看唯一的单元格,这是一些选项:

  • 创建范围
  • 在其中放入一些随机值
  • 使用
    工作表function.CountIf()的内置公式查看值是否唯一
    
  • 如果它是唯一的,请将其涂成洋红色:

  • 样本数据背后的逻辑是什么?另外,嵌套循环中的逻辑是什么,因为我认为不需要复制粘贴?
    Option Explicit
    
    Public Sub GenerateRandoms()
    
        Dim someRange As Range
        Set someRange = Range("A1:F20")
        someRange.Clear
    
        Dim myCell As Range
    
        For Each myCell In someRange
            myCell = MakeRandom(1, 50)
        Next myCell
    
        Dim someUniqueValues As Range
    
        For Each myCell In someRange
            If WorksheetFunction.CountIf(someRange, myCell) = 1 Then
                myCell.Interior.Color = vbMagenta
            End If
        Next myCell
    
    End Sub
    
    Private Function MakeRandom(down As Long, up As Long) As Long
        MakeRandom = CLng((up - down) * Rnd + down)
    End Function