excel中2列内容的每种可能的组合

假设我有2列:

第一列(包含1000行):

U-0001 U-0002 

第二列(包含10行):

 B01 B02 B03 

现在,我想要生成这样的两列(10 * 1000 = 10000行):

 U-0001 B01 U-0001 B02 U-0001 B03 U-0002 B01 U-0002 B02 U-0002 B03 

这应该做到这一点:

 Sub combineTwoCol() ' i,j,k are counters for first col, second col and the answer col in order. k = 1 For i = 1 To 1000 For j = 1 To 10 Cells(k, "C").Value = Cells(i, "A").Value Cells(k, "D").Value = Cells(j, "B").Value k = k + 1 Next j Next i End Sub 

编辑:你应该注意到,我认为你只有这两列是文件,如果没有改变“A”和“B”到你需要的corrosponding列。 和“C”和“D”到你想要的两个输出列。

如果10和1000只是例子,而不是真正的值,你总是可以像这样dynamic地find它们:

 'this return to the variable the last row in column A LastRowColA = Range("A65536").End(xlUp).Row 

并用LastRowColAreplace1000

这是一个公式版本: –

 =IF(ROW()-ROW($A$1)<COUNTA(A:A)*COUNTA(B:B),OFFSET($A$1,(ROW()-ROW($A$1))/COUNTA(B:B),0)&" "&OFFSET($B$1,MOD(ROW()-ROW($A$1),COUNTA(B:B)),0),"") 

input(说)C1并抄下来。