+ Reply to Thread
Results 1 to 6 of 6

Cross product of data in multiple rows and columns

Hybrid View

  1. #1
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Exclamation Cross product of data in multiple rows and columns

    I have 2 sets of data. One which is variable and the other which will be constant. I need to get a cross product of these 2 data. Please help me in achieving this. I have attached the sheet with data in "Customer" and "Products" tab and the desired result set in "Result Tab".
    Attached Files Attached Files

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Cross product of data in multiple rows and columns

    This is Cartesian product:
    Sub Cartesian()
       Dim cn                          As Object
       Dim rs                          As Object
       Dim sQuery                      As String
       Dim i                           As Long
       
       Const adOpenStatic = 3
       Const adLockReadOnly = 1
       Const adCmdText = 1
       
       sQuery = "SELECT * FROM [Customer$], [Products$]"
       
       Set cn = New ADODB.Connection
       
       With cn
          .Provider = "Microsoft.ACE.OLEDB.12.0"
          .ConnectionString = "Data Source=" & ActiveWorkbook.FullName & ";" & _
                              "Extended Properties=""Excel 12.0 Macro;HDR=YES"""
          .Open
       End With
       
       Set rs = CreateObject("ADODB.Recordset")
       
       rs.Open sQuery, cn, adOpenStatic, adLockReadOnly, adCmdText
       
       With rs
    
          If Not .EOF Then
             
             For i = 1 To .Fields.Count
                Sheets("Result").Cells(1, i).Value = .Fields(i - 1).Name
             Next i
             
             Sheets("Result").Cells(2, 1).CopyFromRecordset rs
    
          End If
          .Close
    
       End With
       cn.Close
    End Sub
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  3. #3
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Cross product of data in multiple rows and columns

    @Izandol: Thanks a ton! This is exactly what i needed! However, i am facing a small issue here. I missed out a column in "Customer" tab which also contains a date field. In the "Result" tab the date format is not retained. I need to change the format to date each time in the result set. Please let me know if this can be fixed?

  4. #4
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Cross product of data in multiple rows and columns

    Which column will have date field in result? You may simply add formatting at end of routine:
    Sheets("Result").Range("E:E").Numberformat = "dd-mmm-yyyy"
    for example.

  5. #5
    Registered User
    Join Date
    12-24-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    99

    Re: Cross product of data in multiple rows and columns

    Quote Originally Posted by Izandol View Post
    Which column will have date field in result? You may simply add formatting at end of routine:
    Sheets("Result").Range("E:E").Numberformat = "dd-mmm-yyyy"
    for example.
    Thanks! Its working fine now. I modified the column range and date format accordingly

  6. #6
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Cross product of data in multiple rows and columns

    Hi..

    I am a little slow.. but here is maybe an alternative..

    In the attachment: Press the "Click!" button to see the results on "Sheet1"

    Private Sub CommandButton1_Click()
        Dim rng1 As Range, i As Long
        Set rng1 = Sheets("Products").Range("A1").CurrentRegion.Offset(1).Resize(Sheets("Products").UsedRange.Rows.Count - 1)
        With Sheets("Sheet1")
            .Range("A1").Resize(1, 2).Value = Sheets("Customer").Range("A1:B1").Value
            .Range("C1").Resize(1, 4).Value = Sheets("Products").Range("A1:D1").Value
            .Columns.AutoFit
            For i = 2 To Sheets("Customer").Range("A" & Rows.Count).End(xlUp).Row
                .Range("A" & .Range("A" & Rows.Count).End(xlUp).Row).Offset(1).Resize(Sheets("Products").UsedRange.Rows.Count - 1, 2).Value _
                        = Array(Cells(i, 1), Cells(i, 2))
                .Range("C" & .Range("C" & Rows.Count).End(xlUp).Row).Offset(1).Resize(rng1.Rows.Count, 4).Value = rng1.Value
            Next i
        End With
    End Sub
    Attached Files Attached Files
    Last edited by apo; 02-12-2014 at 07:56 AM. Reason: OPtimise

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 9
    Last Post: 12-15-2013, 10:05 PM
  2. [SOLVED] Columns don't add up in cross-checking with Rows
    By steve@stanley in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 02-04-2013, 02:02 PM
  3. Cross Categorize Product Data - Who can help with this?
    By LionBear in forum Excel General
    Replies: 1
    Last Post: 06-16-2012, 10:25 AM
  4. Cross referencing between columns and rows?
    By mike ryan in forum Excel General
    Replies: 2
    Last Post: 12-29-2011, 05:13 PM
  5. Replies: 2
    Last Post: 04-02-2009, 05:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1