+ Reply to Thread
Results 1 to 4 of 4

Multiple rows into one row / multiple columns - this one is tricky

Hybrid View

  1. #1
    Registered User
    Join Date
    05-15-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    3

    Multiple rows into one row / multiple columns - this one is tricky

    Dear all,
    I have no macro experience and would greatly appreciate it if you could look at the attachment.
    It's seems rather complicated (to me anyway).

    Your help would be greatly appreciated
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,715

    Re: Multiple rows into one row / multiple columns - this one is tricky

    Try this macro

    for it to work you must name the input range "SourceTable" (A1:I10 in your example, ie including the headings row).

    select the first cell where you want the output to begin (cell A14 in your example) and run the macro


    Sub create_table()
    Dim x As Long, Vessel As String, BLDate, n As Long, StartCell, ColNum As Long, ColCount As Long, SourceCol As Long
    
    Vessel = Range("SourceRange").Rows(2).Columns(1).Value
    BLDate = Range("SourceRange").Rows(2).Columns(2).Value
    StartCell = ActiveCell.Address
    
    'type 1st set of column headings
    For n = 1 To Range("SourceRange").Columns.Count
        ActiveCell.Offset(0, n - 1).Value = Range("SourceRange").Columns(n).Rows(1).Value
    Next n
    
    ActiveCell.Offset(1, 0).Select
    ColCount = 2
    ColNum = 2
    
    
    For x = 2 To Range("SourceRange").Rows.Count
        If Range("SourceRange").Columns(1).Rows(x).Value = Vessel And Range("SourceRange").Columns(2).Rows(x).Value = BLDate Then
        ActiveCell.Value = Vessel
        ActiveCell.Offset(0, 1).Value = BLDate
            For n = 3 To Range("SourceRange").Columns.Count
                ActiveCell.Offset(0, ColNum).Value = Range("SourceRange").Columns(n).Rows(x).Value
                ColNum = 1 + ColNum
            Next n
            If ColNum > ColCount Then ColCount = ColNum
        Else
            Vessel = Range("SourceRange").Rows(x).Columns(1).Value
            BLDate = Range("SourceRange").Rows(x).Columns(2).Value
            If ActiveCell.Value <> "" Then ActiveCell.Offset(1, 0).Select
            ColNum = 2
            n = 3
            x = x - 1
        
        End If
    Next x
    
    Range(StartCell).Select
    x = 3
    For n = 2 To ColCount - 1
        ActiveCell.Offset(0, n).Value = Range("SourceRange").Columns(x).Rows(1).Value
        If x = Range("SourceRange").Columns.Count Then x = 3 Else x = x + 1
    Next n
    End Sub

  3. #3
    Registered User
    Join Date
    05-15-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Multiple rows into one row / multiple columns - this one is tricky

    Wow Nicky! I can't thank you enough for taking the time to write that.
    I'm about to give it a go...
    Again, you've made such an effort and I'm very grateful!

  4. #4
    Registered User
    Join Date
    05-15-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Multiple rows into one row / multiple columns - this one is tricky

    Hi Nicky, It worked!!! I'm just so thankful!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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