Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have 2 sheets in excel, each with 10 rows. Is there some sort of "cross-join" formula/macro in Excel that allows me to create a 3rd sheet with all rows cross-joined (100 rows in total).

share|improve this question

3 Answers 3

up vote 4 down vote accepted

This article contains a script that might help :

Need a cross join of the rows from two excel sheets

share|improve this answer

Here is a pretty good tutorial: http://www.digdb.com/excel_add_ins/join_merge_tables_lists/

share|improve this answer
    
@ubunti: this article describes merge-join, not cross-join. –  harrymc Feb 8 '10 at 11:43

If using OLEDB is not an options below function can be used to generate cross product

Option Base 1

Function Cross_Product_range(r1 As Range, r2 As Range) As Variant
    Cross_Product_range = Cross_Product_array(r1.Value, r2.Value)
End Function

Function Cross_Product_array(a1 As Variant, a2 As Variant) As Variant
    On Error GoTo ErrorHandler
    Dim TempArray(), k, m
    ReDim TempArray(UBound(a1) * UBound(a2), UBound(a1, 2) + UBound(a2, 2))
    k = 1
    For i = 1 To UBound(a1)
        For j = 1 To UBound(a2)
            m = 1
            For u = 1 To UBound(a1, 2)
                TempArray(k, m) = a1(i, u)
                m = m + 1
            Next
            For u = 1 To UBound(a2, 2)
                TempArray(k, m) = a2(j, u)
                m = m + 1
            Next
            k = k + 1
        Next
    Next
    Cross_Product_array = TempArray
    Exit Function
    ErrorHandler:
    Debug.Print Err
End Function

Once above function is added to vba module, To produce cross product of A1:B5 and C1:D5 select target cells (say E1:H25) and enter formula and use ctrl + shift + enter

=Cross_Product_range(A1:B5,C1:D5)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.