我在Excel中有两列具有不同的值:
A 1
B 2
C 3
现在,我需要将第一列的每个单元格与第二列的每个单元格配对。所以它看起来像这样:
A 1
A 2
A 3
B 1
B 2
B 3
C 1
C 2
C 3
你知道我该怎么办?
谢谢你堆
答案 0 :(得分:1)
使用 A 和 B 列中的数据尝试使用此短宏:
Sub MakeCombinations()
Dim Na As Long, Nb As Long
Dim i As Long, j As Long, K As Long
Dim rc As Long
K = 1
rc = Rows.Count
Na = Cells(rc, 1).End(xlUp).Row
Nb = Cells(rc, 2).End(xlUp).Row
For i = 1 To Na
For j = 1 To Nb
Cells(K, 3) = Cells(i, 1)
Cells(K, 4) = Cells(j, 2)
K = K + 1
Next j
Next i
End Sub
修改#1:强>
要在没有 VBA 的情况下执行此操作,请在 C1 中输入:
=INDEX(A:A,ROUNDUP(ROW()/COUNTA(B:B),0),1)
并向下复制并在 D1 中输入:
=INDEX(B:B,MOD(ROW()-1,COUNTA(B:B))+1,1)
并复制下来:
答案 1 :(得分:0)
我用数组修改了Gary的答案。由于我的Mac没有Excel,因此未进行测试。
Sub MakeCombinations()
Dim Ary_a As Variant, Ary_b As Variant, Ary as Variant
Dim i As Long, j As Long
Ary_a = range(Cells(rows.count, 1).End(xlUp).Row, 1).value
Ary_b = range(Cells(rows.count, 2).End(xlUp).Row, 2).value
For i = lbound(ary_a) To ubound(ary_a)
For j = lbound(ary_b) To ubound(ary_b)
if not isarray(ary) then
redim ary(1, 0)
else
redim preserve ary(1, ubound(ary, 2)+1)
end if
ary(0, ubound(ary, 2)) = ary_a(i)
ary(1, ubound(ary, 2)) = ary_b(j)
Next j
Next i
cells(1, 4).resize(ubound(ary, 2)+1, ubound(ary, 1)+1).value = application.transpose(ary)
End Sub