+ Reply to Thread
Results 1 to 9 of 9

Unpivot and stack data

  1. #1
    Registered User
    Join Date
    04-26-2013
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Unpivot and stack data

    Hi there,

    I hope you guys can help me here - i would like to normalise and stack the following data:

    table 1.jpg

    to this format using vba:

    table2.jpg

    I'm not so familiar with vba/programming but I would like to learn. thanks so much..

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,333

    Re: Unpivot and stack data

    Hi yching,

    After many questions similar to this, where people wanted to convert Crosstab style table to better formatted Excel tables, I wrote some generalized code. It will convert Crosstab tables but you present a newer problem. The merged cells above the table showing year present a real problem. If you can remove those and make them like my sheet2 I can do the rest. See the attached.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    04-26-2013
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Unpivot and stack data

    Thanks MarvinP.

    I still have a few questions here - If I have the following table, do I still have to change the structure of my table to match exactly like yours before running the macro?
    2010 2010 2010 2011 2011 2011
    Name A B C A B C
    John 1 4 7 10 12 15
    Alan 2 5 8 11 13 16
    Bryan 3 6 9 11 14 17

    Also, why must I enter fixed column=2?

    Thanks

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,333

    Re: Unpivot and stack data

    Hi,

    The way my macro works is this. I assume row 1 has the headers that go with the columns that need repeating. In the normal Crosstab table there are names down the rows and dates across the top in row 1. Sometimes there are two or three columns that are needed to repeat. The reason the macro stops and asks is to allow the user to tell how many columns are "fixed" and need repeating.

    By reformatting your data and having the first column of Name and the second of Year it then fit the format my macro could deal with. I haven't seen many tables like yours where you have two levels of column headings. Maybe if this was a standard, I'd add a second input box to my macro and deal with your structure also.

    In most Excel problems the work gets a lot harder if merged cells are involved. By repeating your 2010 and 2011 like you suggest above might help but still doesn't fit the format that my macro expects to deal with.

    I hope this explains more of what my macro does and why I need it like Sheet2. If not, keep asking. If you get lots of data in the exact format you suggested in post 1 then I might try to improve my code to fit your problem. Let me know.

  5. #5
    Registered User
    Join Date
    04-26-2013
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Unpivot and stack data

    Hi MarvinP,

    Thanks so much for your answer. I kind of understand what each step is doing by pressing F8.

    Yes my table structure is a bit complex, my real data actually has 3 level of column headings and another 3 level of row headings.

    I have been cracking my head but still no clue about how to solve this. So far I'm only manage to unpivot my data if it is a 1 level of column heading and 1 level of row heading..

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,333

    Re: Unpivot and stack data

    Give me an example of what your real data looks like and I'll scratch my head a bit to see if my code can deal with your problem.

    You can attach a sample file by clicking on "Go Advanced" and then the Paper Clip Icon above the advanced message area.

  7. #7
    Registered User
    Join Date
    04-26-2013
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Unpivot and stack data

    Sample_Needs_To_Unpivot.xlsx

    I have attached a sample here.

    It's not the real data but it has the same structure and format.

    The numbers in the sample are not correct as I randomly generated them but it shouldn't cause any problem.

    Thanks a lot.

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,333

    Re: Unpivot and stack data

    OK yching here you go,

    I've added two subs to do your sheets. The first merges the column headers together into a single cell. The second fills in the blanks in column A.
    To test it copy your Sheet1 data (all cells) to a new sheet. Then run the macro called "DoItAll". You need to tell the macro there are 3 fixed columns in your data.

    If you want to put a new column in the converted table of Year, I'd suggest using a formula of =Right(D2,4) and fill it down.

    I'm off to bed now in Seattle time. I hope this works for you. Remember to click the "Add Reputation" star below this post for all the work I may have saved you.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    04-26-2013
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Unpivot and stack data

    Thanks MarvinP,

    I gave you a reputation point from another post (hope that helps?) as I wasn't able to find the "Add Reputation" button from my post.

    Anyways night and thanks so much

+ 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. how to unpivot
    By naveendk55 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-23-2014, 09:55 AM
  2. Unpivot Data
    By Olly in forum Tips and Tutorials
    Replies: 0
    Last Post: 04-02-2014, 05:33 PM
  3. Need 'unpivot' help for attached spreadsheet data.
    By ReneeM787 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-09-2012, 12:21 PM
  4. Stack chart to be used for my data?
    By westbay in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 09-20-2011, 07:29 PM
  5. Multiple data in stack column chart
    By SMueller in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 06-30-2006, 05:00 PM

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