+ Reply to Thread
Results 1 to 6 of 6

Unpivot Macro

  1. #1
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Unpivot Macro

    I looked around for something to help me unpivot data and couldn't find anything to meet what I needed. So I created this macro and I am sharing it so others can use it and to see if there are any improvements I could make.

    This unpivot is specifically made for data that has several columns of information before the date columns start. So I may have a column for name, a column for department, a column for accounting cost center, and then I would start having columns for each month with data filled in for sales dollars for each of those months. While this is a great way to visually present data, it is horrible to try to analyze it in this format.

    Some codes I have seen unpivot by looking at each cell and deciding what to do with it. In my case I have to unpivot data that has several thousand rows with several years of date columns off to the right. Looking cell by cell simply doesn't work with that much data to transform. This macro uses arrays to transform the data in larger chunks, looking at whole rows instead of looking cell by cell. I tested it with 2,000 rows of data and 24 columns of dates off to the right and it worked in a flash.

    That said I am sure there can be improvements made to the code. If you have ideas to improve it, make a comment, if you need something like this, use it.

    Please Login or Register  to view this content.
    Some people volunteer in soup kitchens or hospitals. I choose to make the world better by trying to help you with Excel. We're all learning.

    <---Click * Add Reputation for all helpful comments. It's like giving a smile.
    Forum Rules: How to mark your post [Solved] and have a happier, Excel enriched life.

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

    Re: Unpivot Macro

    Hi Nigel,

    By your thread title I thought you were going to show how to Drill Down and get the data that made up a cell in your pivot table, like shown in:
    https://exceljet.net/lessons/how-to-...-a-pivot-table

    Then I discovered a NEW Pivot Table feature for Excel 2013 called Quick Explore that allows Drilling Down and Drilling Up at:
    https://support.office.com/en-us/art...rs=en-US&ad=US

    I don't understand your code above well enough without having a sample workbook to run it on. Could you please attach a sample with the code included so I can see what it does?
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: Unpivot Macro

    Here is an example sheet. I know there are several understandings of the term unpivot. In my case I have a data dump from a system that gives me data in what looks like a pivot table format. There are several starting columns and then each date has its own column. While this looks nice and looks like a pivot table, it isn't one. It can't be analyzed very easily the way it is. What I needed was a way to get the data back to the format where I could analyze it, a database type format. I need a new row for each date and value. Once it is back in that format I can put a real pivot table on it and make it look that same way with a new column for each date, but it has much more analytical power because the underlying data is in a normal database type format. I can filter and compare by cost center or department or group by year.

    Hopefully that explains it a bit better along with the attached sheet.
    Attached Files Attached Files

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

    Re: Unpivot Macro

    Hi,

    A few years ago I wrote a macro I called MakeCrosstabATable which I think does the same as your Unpivot macro. I call the starting type of structure a Crosstab Table and you call it a Pivot table structure. Mine looks like this.

    Please Login or Register  to view this content.
    I've included it in the following attachement and have used it to show the differences between your method and mine.
    Attached Files Attached Files
    Last edited by MarvinP; 09-02-2015 at 11:02 AM.

  5. #5
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: Unpivot Macro

    I like your approach. I made chunks for each row, but I hadn't thought of repeating all of the rows for each date. It works well.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Unpivot Macro

    An alternative, but some of the columns are nil, but you have filled them with numbers.
    Attached Files Attached Files
    Last edited by AB33; 09-02-2015 at 05:29 PM.

+ 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 normalise (unpivot) 13 columns from 16 in vba?
    By gerotutu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-15-2015, 04:52 PM
  2. Unpivot Data
    By naveeddil in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 07-07-2015, 08:04 AM
  3. Unpivot and stack data
    By yching in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-14-2015, 05:18 AM
  4. how to unpivot
    By naveendk55 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-23-2014, 09:55 AM
  5. Unpivot Data
    By Olly in forum Tips and Tutorials
    Replies: 0
    Last Post: 04-02-2014, 05:33 PM
  6. 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
  7. "Unpivot Technique" - Changing a Table Into A List
    By frankybenali in forum Excel General
    Replies: 1
    Last Post: 07-29-2005, 10:05 AM

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