我有几行(行数应该是动态的)数据,我想将它们组合起来并获得每个唯一组合。我可以针对特定数量的数据行执行此操作,但是当行数不固定时,我该怎么办?
例如:
输入数据:
Row 1 : Correct Wrong Blank
Row 2 : Correct1 Wrong1 Blank1
Row 3 : Ok Cancel
输出应该是这样的:
Correct Correct1 Ok
Correct Correct1 Cancel
Correct Wrong1 Ok
Correct Wrong1 Cancel
Correct Blank1 Ok
Correct Blank1 Cancel
Wrong Correct1 Ok
Wrong Correct1 Cancel
Wrong Wrong1 Ok
Wrong Wrong1 Cancel
Wrong Blank1 Ok
Wrong Blank1 Cancel
Blank Correct1 Ok
Blank Correct1 Cancel
Blank Wrong1 Ok
Blank Wrong1 Cancel
Blank Blank1 Ok
Blank Blank1 Cancel
答案1
下列虚拟专用网络宏将从中读取数据工作表1并将结果放入工作表2:
Sub Combinations()
Dim r1 As Range, r2 As Range, r3 As Range
Dim v1 As Variant, v2 As Variant, v3 As Variant
Dim rw1 As Range, rw2 As Range, rw3 As Range
Dim s As Worksheet, K As Long
Set s = Sheets("Sheet2")
K = 1
With Sheets("Sheet1")
Set rw1 = .Rows(1).Cells.SpecialCells(xlCellTypeConstants)
Set rw2 = .Rows(2).Cells.SpecialCells(xlCellTypeConstants)
Set rw3 = .Rows(3).Cells.SpecialCells(xlCellTypeConstants)
End With
For Each r1 In rw1
v1 = r1.Value
For Each r2 In rw2
v2 = r2.Value
For Each r3 In rw3
v3 = r3.Value
s.Cells(K, 1) = v1
s.Cells(K, 2) = v2
s.Cells(K, 3) = v3
K = K + 1
Next r3
Next r2
Next r1
End Sub