Results 1 to 2 of 2

Moving Vertical Data into Horizontal Data

Threaded View

  1. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Moving Vertical Data into Horizontal Data

    Hi Cmaskew,

    The following macro assumes:
    1. The SSN entries are all grouped together in consecutive rows.
    2. You have headers in row 1 of Sheet2 for the new list to be created.
    3. There are only three possible relation types (00, 01 and 02).

    It even handles instances where there are more than two children (just keeps adding them in columns further to the right (leaving an empty column between entries).
    Sub reorg()
    Dim i As Long, LR As Long, NR As Long, NC As Long
    Dim ws1 As Worksheet, ws2 As Worksheet
    
    Set ws1 = Sheets("Sheet1")
    Set ws2 = Sheets("Sheet2")
    
    LR = ws1.Range("A" & Rows.Count).End(xlUp).Row
    
    With ws2
        NR = .Range("A" & .Rows.Count).End(xlUp).Row + 1
        For i = 3 To LR
            Select Case ws1.Cells(i, 2).Value
                Case Is = 0
                    .Cells(NR, 1).Value = ws1.Cells(i, 3).Value
                    .Cells(NR, 2).Value = ws1.Cells(i, 4).Value
                    NR = NR + 1
                Case Is = 1
                    .Cells(NR - 1, 4).Value = ws1.Cells(i, 3).Value
                    .Cells(NR - 1, 5).Value = ws1.Cells(i, 4).Value
                Case Is = 2
                    NC = WorksheetFunction.Max(7, .Cells(NR - 1, 255).End(xlToLeft).Column + 2)
                    .Cells(NR - 1, NC).Value = ws1.Cells(i, 3).Value
                    .Cells(NR - 1, NC + 1).Value = ws1.Cells(i, 4).Value
            End Select
        Next i
    End With
    End Sub
    I've attached the sample spreadsheet I used. You can open it and run the reorg macro. Hope that helps!
    Attached Files Attached Files
    Last edited by Paul; 12-20-2011 at 01:23 AM. Reason: Modified code to handle when there are children but no spouse.

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