我有一个包含两列的表:
Height Width
400 200
500 300
600 400
700
800
...
需要在它旁边创建第二个表格,以便为宽度列中的每个值重复整个列高度,同时复制,基本上可以获得所有可能的组合:
Height Width
400 200
500 200
600 200
700 200
800 200
400 300
500 300
600 300
700 300
800 300
答案 0 :(得分:2)
我能想到运行嵌套循环的最简单方法。您可能需要根据工作表的结构修改下面的代码,但它应该可以帮助您。
Sub loops()
Dim n_height, n_width, c As Integer
With ThisWorkbook.Sheets("Sheet1")
n_height = .Cells(Rows.Count, 1).End(xlUp).Row 'Assuming height is in column A
n_width = .Cells(Rows.Count, 2).End(xlUp).Row 'Assuming width is in column B
c = 2
For i = 2 To n_height
For j = 2 To n_width
.Range("D" & c).Value = .Range("A" & i).Value 'Prints heights in column D
.Range("E" & c).Value = .Range("B" & j).Value 'Prints widths in column E
c = c + 1
Next j
Next i
End With
End Sub