+ Reply to Thread
Results 1 to 19 of 19

Convert data from columns to rows with M code

  1. #1
    Registered User
    Join Date
    06-09-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    10

    Convert data from columns to rows with M code

    I found a partial solution in a thread named convert-data-from-vertical-to-horizontal-based-on-column-a.
    The post covers most of my problem, but I need a slightly different output column. I would like all the values to be concatenated with a "|" between them.

    This is my input:
    TT Test
    Assembly Final
    Assembly Preliminary
    Assembly Set out
    Coating Application
    Coating Blast/Prep
    Coating Coating
    Coating Final

    I would like a table that shows:

    Column 1 Column 2
    Assembly |Final|Preliminary|Set out
    Coating |Application|Blast/Prep|Coating|Final

    Thanks for your help!
    Last edited by JewelDarby; 11-12-2019 at 12:46 AM.

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,704

    Re: Convert data from columns to rows with formula

    It would help if you attached a sample Excel workbook, so we can see exactly how your data is arranged.

    To do this, click on Go Advanced (below the Edit Window) while you are composing a reply, then scroll down to and click on Manage Attachments and the Upload window will open. Click on Browse and navigate to (and double-click) the file icon that you want to attach, then click on Upload and then on Close this Window to return to the Edit window. When you have finished composing your post, click on Submit Post.

    Don't try to use the Paperclip icon for attachments, as it doesn't work in this forum.

    Hope this helps.

    Pete

  3. #3
    Registered User
    Join Date
    06-09-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Convert data from columns to rows with formula

    Attaching sample
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    06-09-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Convert data from columns to rows with formula

    Do you think 6 steps to post an attachment might be excessive? *WinkWink NodNod*
    But thanks none the less for detailed steps.
    Last edited by JewelDarby; 11-09-2019 at 11:56 PM.

  5. #5
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,445

    Re: Convert data from columns to rows with formula

    And they were explained in the yellow banner which seems to go unnoticed

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,410

    Re: Convert data from columns to rows with formula

    Do you think 6 steps to post an attachment might be excessive? *WinkWink NodNod*
    We can't make it any simpler - sorry. That's why we put that yellow banner up at the top (the one you didn't see).
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  7. #7
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    43,984

    Re: Convert data from columns to rows with formula

    1. Not many Excel forums allow attacments at all, so...

    2. Option 1. VBA.

    3. Option 2 A helper column.

    Which do you prefer?
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  8. #8
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,704

    Re: Convert data from columns to rows with formula

    You can put this formula in D3 of the Input Data sheet:

    =IF(B3="","",IF(COUNTIF(B$3:B3,B3)=1,MAX(D$2:D2)+1,MAX(D$2:D2)&"_"&COUNTIF(B$3:B3,B3)))

    and then copy down to the bottom of your data (or beyond, if you wish). In the other sheet you can use these formulae:

    B3: =IFERROR(INDEX('Input Data'!B:B,MATCH(ROWS($1:1),'Input Data'!$D:$D,0)),"")

    C3: =IF(B3="","","|"&INDEX('Input Data'!$C:$C,MATCH(ROWS($1:1),'Input Data'!$D:$D,0))&IFERROR("|"&INDEX('Input Data'!$C:$C,MATCH(ROWS($1:1)&"_2",'Input Data'!$D:$D,0)),"")&IFERROR("|"&INDEX('Input Data'!$C:$C,MATCH(ROWS($1:1)&"_3",'Input Data'!$D:$D,0)),"")&IFERROR("|"&INDEX('Input Data'!$C:$C,MATCH(ROWS($1:1)&"_4",'Input Data'!$D:$D,0)),"")&IFERROR("|"&INDEX('Input Data'!$C:$C,MATCH(ROWS($1:1)&"_5",'Input Data'!$D:$D,0)),""))

    That might look a bit daunting, but it is more straightforward if I show it like this:

    =IF(B3="","","|"&INDEX('Input Data'!$C:$C,MATCH(ROWS($1:1),'Input Data'!$D:$D,0))
    &IFERROR("|"&INDEX('Input Data'!$C:$C,MATCH(ROWS($1:1)&"_2",'Input Data'!$D:$D,0)),"")
    &IFERROR("|"&INDEX('Input Data'!$C:$C,MATCH(ROWS($1:1)&"_3",'Input Data'!$D:$D,0)),"")
    &IFERROR("|"&INDEX('Input Data'!$C:$C,MATCH(ROWS($1:1)&"_4",'Input Data'!$D:$D,0)),"")
    &IFERROR("|"&INDEX('Input Data'!$C:$C,MATCH(ROWS($1:1)&"_5",'Input Data'!$D:$D,0)),""))

    This can accommodate up to 5 items joined together with the | character separating them, but it is quite easy to add more terms if required. If you had a later version of Excel, you could use the TEXTJOIN function to simplify this.

    Copy both formulae down as far as you need them.

    Hope this helps.

    Pete

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this !


    As a starter :

    PHP Code: 
    Sub Demo1()
        
    Dim VR&
            
    = [B2].CurrentRegion.Value2
        With CreateObject
    ("Scripting.Dictionary")
            For 
    2 To UBound(V):  .Item(V(R1)) = .Item(V(R1)) & "|" V(R2):  Next
            
    [B3:C3].Resize(.Count).Value2 Application.Transpose(Array(.Keys, .Items))
            If .
    Count UBound(VThen Cells(+ .Count2).Resize(UBound(V) - - .Count2).Clear
           
    .RemoveAll
        End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  10. #10
    Registered User
    Join Date
    06-09-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Hi ! Try this !

    Marc L,
    I cannot get this macro to run. I get a Type Mismatch error on this line:
    For R = 2 To UBound(V): .Item(V(R, 1)) = .Item(V(R, 1)) & "|" & V(R, 2): Next

    In the original thread, a Power Query was used and I was hoping for that approach: convert data from vertical to horizontal based on column a

  11. #11
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,704

    Re: Hi ! Try this !

    In your original post, you asked to do this with a formula. I gave you a solution which actually uses 3 formulae - did you try it?

    Pete

  12. #12
    Registered User
    Join Date
    06-09-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Convert data from columns to rows with formula

    I cannot get this macro to run. I get a Type Mismatch error on this line:
    For R = 2 To UBound(V): .Item(V(R, 1)) = .Item(V(R, 1)) & "|" & V(R, 2): Next

  13. #13
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,859

    Re: Convert data from columns to rows with formula

    If you are willing to go the Power Query method then here is the Mcode. Look at the links in my signature for how to apply. Also attached file for review.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Convert data from columns to rows with formula

    VBA for your attachement.
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    06-09-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Convert data from columns to rows with formula

    Alan,
    This is oh so close. The sample file you returned did look right.

    But when I paste my full table in, the delimiters don't seem to appear, just "test types" in various columns.
    I did view your "how to use Mcode" so I could try again, but can't seem to make it work.

    However, with what you supplied, maybe I can make it work if I noodle it.
    Thank you SO MUCH for what you provided!

    Let me know if you need the full table to work with.
    Thank you again,
    Jewel

  16. #16
    Registered User
    Join Date
    06-09-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Convert data from columns to rows with formula

    This errors out here for me:

    Subscript out of range
    a = .[b2].CurrentRegion.Value

    with the table selected or not

  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,516

    Re: Convert data from columns to rows with M code

    Impossible...
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    06-09-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Convert data from columns to rows with M code

    That worked perfectly.
    I will review the code and try to learn.
    Thank you SO MUCH.

  19. #19
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Quote Originally Posted by JewelDarby View Post
    I get a Type Mismatch error
    As no issue with your attachment on my side …

+ 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. Need a formula to convert to columns to two rows
    By Learner326 in forum Excel General
    Replies: 2
    Last Post: 09-19-2014, 03:46 AM
  2. Excel 2007 : Convert data from columns to rows
    By thandanit in forum Excel General
    Replies: 3
    Last Post: 03-27-2011, 10:55 PM
  3. Excel 2007 : Convert data from columns to rows
    By thandanit in forum Excel General
    Replies: 1
    Last Post: 03-17-2011, 03:41 AM
  4. Convert a set of data from rows to columns
    By naferterri in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-19-2010, 08:33 PM
  5. Convert data from columns to rows
    By ClikClak in forum Excel General
    Replies: 5
    Last Post: 07-19-2010, 12:26 PM
  6. convert data from columns to rows
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-03-2009, 11:14 AM
  7. Convert Columns to rows with DATA
    By raed_237 in forum Excel General
    Replies: 6
    Last Post: 08-05-2008, 04:18 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