如何获得excel中的列像笛卡尔产品?
我有2列:
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
提前致谢!
以下是关于一种可能性的几点说明。
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