+ Reply to Thread
Results 1 to 6 of 6

Transpose Columns into Rows based on Unique Identifier for n columns

Hybrid View

bhavt010785 Transpose Columns into Rows... 03-01-2017, 06:00 AM
apo Re: Transpose Columns into... 03-01-2017, 06:44 AM
bhavt010785 Re: Transpose Columns into... 03-01-2017, 07:41 AM
mike7952 Re: Transpose Columns into... 03-01-2017, 09:56 AM
bhavt010785 Re: Transpose Columns into... 03-01-2017, 11:50 AM
mike7952 Re: Transpose Columns into... 03-01-2017, 11:55 AM
  1. #1
    Registered User
    Join Date
    03-01-2017
    Location
    London
    MS-Off Ver
    2010
    Posts
    3

    Transpose Columns into Rows based on Unique Identifier for n columns

    Hi,

    I'm new to VBA and am struggling to find any solutions for this problem - I have a spreadsheet which contains about 10k rows and 21 columns. The data has identifiers in Column C and I'm trying to write some kind of VBA Code to transpose the data into a new sheet based on the identifier in Column C.

    Below is how the data is set out for column C
    C
    1111111
    1111111
    1111111
    1111111
    1111111
    1111112
    1111113
    1111113
    1111113
    1111113
    1111113
    1111113
    1111113
    1111113
    1111114
    1111114
    1111115

    The code I am trying to get is to copy and paste values into "Sheet 2" which goes down the columns and transposes until the ID in column C is different from the next one. The number of Ids in column C that will be the same ranges from 1 to 10. so it will look like the below (obviously with lots of columns added):
    C
    1111111
    1111112
    1111113
    1111114
    1111115

    I have attached a copy of a sample of the spreadsheet that should make it clearer. I have only included 16 columns but there will be 21 but if there is a way of making this work for n-columns that would be amazing! I'm happy with having loads and loads of columns to work with...

    Thanks!!
    Attached Files Attached Files

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

    Re: Transpose Columns into Rows based on Unique Identifier for n columns

    HI..

    Can you reattach the sample workbook but this time include the desired result for that sample set on sheet2

  3. #3
    Registered User
    Join Date
    03-01-2017
    Location
    London
    MS-Off Ver
    2010
    Posts
    3

    Re: Transpose Columns into Rows based on Unique Identifier for n columns

    Hi,

    Thanks for coming back so quickly and apologies - that would have made things clearer! I've attached a file with the output in Sheet2.

    Thank you very much!
    Attached Files Attached Files

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Transpose Columns into Rows based on Unique Identifier for n columns

    This will work for your example worksheet.

    Sub abc()
     Const shData As String = "Sheet1"
     Const shOutput As String = "Sheet2"
     Dim arr, w, key
     Dim i As Long, ii As Long
     
     With Worksheets(shData)
        arr = .Range("a1").CurrentRegion.Value
     End With
     With CreateObject("scripting.dictionary")
        For i = 2 To UBound(arr)
            If Not .exists(arr(i, 3)) Then
                ReDim w(UBound(arr, 2) - 1)
                For ii = 1 To UBound(arr, 2)
                    w(ii - 1) = arr(i, ii)
                Next
            Else
                w = .Item(arr(i, 3))
                For ii = 7 To UBound(arr, 2)
                    ReDim Preserve w(UBound(w) + 1)
                    w(UBound(w)) = arr(i, ii)
                Next
            End If
            .Item(arr(i, 3)) = w
        Next
        i = 2
        For Each key In .keys
            If ii < UBound(.Item(key)) Then ii = UBound(.Item(key)) - 5
            Worksheets(shOutput).Cells(i, 1).Resize(, UBound(.Item(key)) + 1) = .Item(key)
            i = i + 1
        Next
     End With
     With Worksheets(shData)
        .Range("a1:f1").Copy Worksheets(shOutput).Range("a1")
        .Range("g1", .Cells(1, Columns.Count).End(xlToLeft)).Copy Worksheets(shOutput).Range("g1")
     End With
     With Worksheets(shOutput)
        .Range("G1:P1").AutoFill Destination:=.Range("G1").Resize(, ii), Type:=xlFillCopy
     End With
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  5. #5
    Registered User
    Join Date
    03-01-2017
    Location
    London
    MS-Off Ver
    2010
    Posts
    3

    Re: Transpose Columns into Rows based on Unique Identifier for n columns

    Wow - that worked perfectly! Thanks so much!

  6. #6
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Transpose Columns into Rows based on Unique Identifier for n columns

    Your welcome.

+ 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. [SOLVED] Identify difference in value between two columns based on matching of unique identifier
    By rn_ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-09-2017, 07:38 PM
  2. Replies: 0
    Last Post: 02-09-2017, 01:20 PM
  3. Replies: 0
    Last Post: 02-09-2017, 01:10 PM
  4. Replies: 0
    Last Post: 02-09-2017, 01:03 PM
  5. Merging Rows & Columns on Unique Identifier
    By oxv in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-20-2012, 06:52 PM
  6. Transpose Multiple Columns to one Row with Unique Identifier
    By PleaseHelp! in forum Excel General
    Replies: 4
    Last Post: 05-07-2012, 07:43 PM
  7. Replies: 1
    Last Post: 12-07-2009, 07:02 PM

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