2014-10-08 45 views
0

我有兩列在我的excel表,下面的例子: -合併兩個列表進行比較(EXCEL)

typeA  typeB 

cat   product 
fish  organs 
chicken  slaughter 
goat  live 

我需要的細胞相互結合中的每一列,像這樣: -

typeC 

cat_product 
cat_organs 
cat_slaughter 
cat_live 
fish_product 
fish_organs 
... 
... 
goat_slaughter 
goat_live 

我知道我們可以使用CONCATENATE來合併單元格,但是如何在兩列之間進行迭代?列typeA中的單元格中的每個值必須與列typeB中的單元格中的每個值組合。

謝謝!

+0

嘿,@antsemot - 如果我們的答案有幫助,不要忘了投票和/或接受答案,以便其他人可以向他們學習。 – Daniel 2014-10-08 05:18:24

+0

感謝您的接受,@antsemot! – Daniel 2014-10-23 23:41:53

回答

0

以下代碼將創建一個包含所需的所有值的數組(MyArray)。不管你喜歡,你都可以操作這個數組。

Sub Combine() 
Dim MyArray() As String 
Dim i, k As Integer 
Dim msg As String 

Array1 = Array("cat", "fish", "chicken", "goat") 
Array2 = Array("product", "organs", "slaughter", "live") 

i = 1 

k = (UBound(Array1) + 1) * 4 

ReDim MyArray(1 To k) 

For Each x In Array1 

    For Each y In Array2 

     MyArray(i) = x & "_" & y 

     i = i + 1 

    Next 

Next 

'display the results in a MsgBox  

For i = LBound(MyArray) To UBound(MyArray) 
    msg = msg & MyArray(i) & vbNewLine 
Next i 

MsgBox msg 

End Sub 
0

假設cat在A3中,將B3:B6複製到C1並粘貼特殊,移調。然後在C3:

=$A3&"_"&C$1 

並複製和複製到適合。