简体   繁体   中英

Combine two lists to compare (Excel)

I have two columns in my excel sheet, example below:-

typeA       typeB

cat         product
fish        organs
chicken     slaughter
goat        live

I need to combine the cells in each column with each other, like this:-

typeC

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

I know we can use CONCATENATE to combine the cells, but how do i iterate it among the two columns? Each value in the cells in column typeA must be combined with each value in the cells in column typeB.

Thanks!

Assuming cat is in A3, copy B3:B6 to C1 and Paste Special, Transpose. Then in C3:

=$A3&"_"&C$1  

and copy across and down to suit.

The following code creates an array (MyArray) with all the values you need. You can manipulate this array however you like.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM