如何重复另一列中每一行的所有列值

我有一个有两列的表格:

Height Width 400 200 500 300 600 400 700 800 ... 

并且需要在其旁边创build第二个表格,以便在宽度列中为每个值重复整个列高度,同时复制基本上获得每种可能的组合:

 Height Width 400 200 500 200 600 200 700 200 800 200 400 300 500 300 600 300 700 300 800 300 

我可以想到运行嵌套循环的最简单的方法。 您可能需要修改下面的代码,这取决于您的工作表的结构,但它应该让你去。

 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