I have hierarchical data like this
Country Region Category ProgramName
USA North SchoolName A
USA North SchoolName B
USA South SchoolName C
Brasil East SchoolName D
Brasil East CollegeName E
Brasil West CollegeName F
I would like to pivot it into a user readable format.
I am able to build the pivot table, however I would like to use nonnumeric data as the pivot. The VBA code in this answer seems promising but it can only pivot a single nonhierarchical column. How can I achieve my goal?
I couldn't find code lying around on the 'net to do just what you're looking for. It might be possible through some Get & Transform sorcery, but that's not my field of expertise. Because it's an interesting problem and because I can think of use cases for my own projects, here's my take on it.
Disclaimer: this code is hot off the stove and hasn't been thoroughly tested. Use at your own risk.
First, create a new workbook and, on Sheet1, set these values starting at cell A1 (I've added the SubCategory column for testing purposes):
Then, create a class module named CTextTransposer and paste this code into it:
Finally, create a module and paste this code into it:
Run the
TestTextTransposer
sub and observe the results starting onSheet1
, cellH10
. Looking at the test code, you'll see I've used all options offered by the class, plus I've made use of the ranges it returns to do some basic formatting.I won't explain all of the details here, but you'll see it boils down to a few dictionaries and some array manipulations. Hope it helps.
Note: as posted, the classe's dictionaries keyed by strings are case-sensitive, so your source data has to be prepared with this in mind. This can easily parameterized by adding another property to the class.
Here's the end result (with a little more formatting applied):
So from your answer it sounds like you want this:
But PivotTables actually give you a much better way of viewing the exact same information natively, like this:
...the bonus being that there's no repetition of those G rows...instead you get a count. But other than that, you get the exact same information from either. Any particular reason why you don't want the 'native' PivotTable layout?