Results 1 to 2 of 2

Thread: line up columns in Excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2004
    Posts
    14

    line up columns in Excel

    Im writing a program that takes 2 or more recordsets and dumps them into columns in Excel. Im dumping the recordsets one at a time. Once in Excel I need to line up the matching cells and shift non matching cells into a new row. What I need help with is the matching and shifting of the cells. For example if my spreadsheet has 4 columns that looks like this:


    VB Code:
    1. Recordset1     Recordset 2     Recordset3     Recordset4
    2.  
    3.    b              a              b               a
    4.  
    5.    c               b              c               d
    6.  
    7.    d               d              d               f
    8.  
    9.    e               e              f               g
    10.  
    11.     f
    12.  
    13. My program needs to sort the columns so that my spreadsheet looks like this:
    14.  
    15.                    a                               a
    16.  
    17.    b               b               b                      
    18.  
    19.    c                               c              
    20.  
    21.    d               d               d               d
    22.  
    23.    e               e                                            
    24.  
    25.    f                               f               f
    26.  
    27.                                                    g

    Im having trouble writing an excel macro to compare the columns to see if they should line up and how to do the shifting of the cells if they dont match. Any help would be appreciated.
    Last edited by jvdub22; Nov 23rd, 2004 at 01:43 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    I think moving them around wouldnt be the easiest way, I would recommend only putting them into the sheet in the first place if they are appropriate.

    Something a bit like this (not tested):
    VB Code:
    1. 'Load recordsets here
    2.  
    3. Dim RowNo_l as Long
    4.  
    5. RowNo_l = 1
    6. Dim GotValue_b as Boolean
    7. Dim CurrValue_v as Variant
    8. Do While Not(Recordset1.Eof) And Not(Recordset2.Eof) _
    9.       And Not(Recordset3.Eof) And Not(Recordset4.Eof)
    10.   RowNo_l = RowNo_l + 1
    11.   GotValue_b = False       'find the "lowest" value in the recordsets
    12.  
    13.   If Not (Recordset1.Eof) Then
    14.     If Not(GotValue_b) Then
    15.       CurrValue_v = Recordset1(0)  '(change to your field name/number)
    16.     Else
    17.       If (Recordset1(0) < CurrValue_v) Then  CurrValue_v = Recordset1(0)  
    18.       GotValue_b = True
    19.     End If    
    20.   End If
    21. '(repeat the above block of code for all 4 recordsets)
    22.  
    23.  
    24.                                 'put the appropriate data in
    25.   If Not (Recordset1.Eof) Then
    26.     If CurrValue_v = Recordset1(0) Then
    27.        objSheet.Cells(RowNo_l,1) = Recordset1(0)   'change this line (and same below) as apt
    28.        Recordset1.MoveNext
    29.     End If
    30.   End If
    31.   If Not (Recordset2.Eof) Then
    32.     If CurrValue_v = Recordset2(0) Then
    33.        objSheet.Cells(RowNo_l,2) = Recordset2(0)
    34.        Recordset2.MoveNext
    35.     End If
    36.   End If
    37.   If Not (Recordset3.Eof) Then
    38.     If CurrValue_v = Recordset3(0) Then
    39.        objSheet.Cells(RowNo_l,3) = Recordset3(0)
    40.        Recordset3.MoveNext
    41.     End If
    42.   End If
    43.   If Not (Recordset4.Eof) Then
    44.     If CurrValue_v = Recordset4(0) Then
    45.        objSheet.Cells(RowNo_l,4) = Recordset4(0)
    46.        Recordset4.MoveNext
    47.     End If
    48.   End If
    49. Loop

Posting Permissions

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



Click Here to Expand Forum to Full Width