我有 4 列,每列的记录数不同。我想获取由这 4 列组成的新表并获取所有可能的组合。
我的初始四列是:
A 列(段)有 10 条记录。 B 列(BAA 部门)有 14 条记录。 C 列(终端)有 4 条记录。 D列(小时)有24条记录。
结果应该是一个有 4 列表格,有 10*14*4*24 = 13440 行。
您能帮我在 Excel 中做到这一点吗?如果每列的记录数相同,我知道该怎么做。
答案1
您可以根据您的需要调整这个简短的宏:
Sub kombin()
Dim a As Long, b As Long, c As Long, d As Long
Dim pack(1 To 4) As Variant, K As Long
Dim s1 As Worksheet, s2 As Worksheet
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")
K = 1
For a = 2 To 11
pack(1) = s1.Cells(a, 1)
For b = 2 To 15
pack(2) = s1.Cells(b, 2)
For c = 2 To 5
pack(3) = s1.Cells(c, 3)
For d = 2 To 26
pack(4) = s1.Cells(d, 4)
s2.Range("A" & K & ":D" & K) = pack
K = K + 1
Next d
Next c
Next b
Next a
End Sub