2016-08-03 192 views
0

我有在Excel中兩列具有不同的值:對細胞在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 

你知道我該怎麼做嗎?

謝謝你滿口

回答

1

隨着列一個嘗試這短短的宏觀數據:

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 

enter image description here

編輯#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) 

抄下:

enter image description here

+0

工程就像一個魅力;-)謝謝你 – Turpan

0

我修改加里的答案與陣列。沒有測試,因爲我的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