如何在excel中获得像柱子这样的笛卡尔积?

时间:2011-02-16 16:05:02

标签: excel

我有两列:

Col1    Col2 
  1       A
  2       B
  3       C

我需要获得的是使用Excel将这些列的所有可能组合到新列中。那可能吗?我没有使用excel的经验。

预期结果:

Col3 Col4 Col5 Col6 Col7 Col8
  1   A     2    A   3    A
  1   B     2    B   3    B
  1   C     2    C   3    C

提前致谢!

1 个答案:

答案 0 :(得分:1)

以下是关于一种可能性的一些注释。

strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
    & ThisWorkbook.FullName _
    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

''Late binding, so no reference is needed

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set rs2 = CreateObject("ADODB.Recordset")

cn.Open strCon


strsql = "SELECT * " _
       & "FROM [Sheet2$a:a]"

rs2.Open strsql, cn, 3, 3

i = 1
Do While Not rs2.EOF
    strsql = "SELECT * " _
           & "FROM [Sheet2$a:a] a, " _
           & "[Sheet2$b:b] b " _
           & "WHERE Col1 = " & rs2!Col1

    rs.Open strsql, cn, 3, 3

    ''Pick a suitable empty worksheet for the results
    Worksheets("Sheet3").Cells(2, i).CopyFromRecordset rs

    i = i + rs.fields.Count

    rs2.movenext
    rs.Close
Loop

''Tidy up
Set rs = Nothing
rs2.Close
Set rs2 = Nothing
cn.Close
Set cn = Nothing