我知道如何创建所有可能的组合,但我很难获得唯一的组合。
在我的例子中,我在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
答案 0 :(得分:0)
如果想要制作一些随机范围并查看唯一的单元格,这是一些选项:
WorksheetFunction.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