2014-10-08 65 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 

并复制和复制到适合。