Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Question: How to Transpose - Multiple Rows/Columns - Convert one column values into row headers

0

Hi All,

I am a bit novice to Excel and trying to find an easy way to Transpose thousands of records into the desired format. Any help (VBA code or pointers) will be of great help.

Below attached file shows the Current data format and the format it needs to be converted in:

Answer
Discuss

Answers

0

Hi

You can do this, without VBA, by just using a Pivot Table (a powerful tool for summarising data in Excel).

The attached version of your sheet has that done (with a small sample of data)- suggest you try that before applying to thousands of records.

Revision 1: After typing my answer, I realised it might be daunting for a "novice" but there's an easy way to use the Pivot Table I created and formatted to suit your needs...

Open up the file attached and (in Sheet 1) you'll see (from cell F10 down and right) what's called a Pivot Table (created by the steps written beyond this revision in italics). It summarises the data in your sample records in A1 to C9 in the form you need.

Now you can change the data it uses easily. Click in F10 (or anywhere in that table) and in the Excel ribbon you'll see two new tabs under PivotTable Tools. Click Analyze then Change Data Source. Under Table and Range, it will say Sheet1!$A$1:$C$9 (your sample data). 

Click in that box then click Sheet 2 and select all the (test) data in Sheet 2 (that data is a bit of a copy of A1:C9 so don't expect it to look realistic!). It should then read Sheet2!$A$1:$C$97. Press enter and the Pivot table on Sheet1 will now show records for 48 employees.

To do that with your real data, indo the source change and copy Sheet1 to the workbook where your thousands of records are. Go to it and change the pivot data source as above (having made sure the column headings for the sheet with thousands are the same as in temp.xlsx). That's it!

Back to the original answer (which I suggest you follow to see how it's done)...

There are a few steps to creating that pivot table but just follow them carefully please on your original temp.xlsx file (it's worth if for other stuff!):

  1. Select the data (A1:A9 in your temp.xlsx file)
  2. On the Insert ribbon, click Pivot Table then under "Choose where..." click Existing Worksheet (so you can see the table alongside your data) and pick in Location the upper left cell for the table (I chose F10 of Sheet 1) then OK. Note: With much more data, you'll need to pick another sheet 
  3. You'll see a box appear the Location and, to the right of your screen a new Section called "PivotTable fields" (this will reappear when you click in the table later).
  4. At the top of that area, you'll see your headings, Employee ID, Status and Date. To create your pivot table:
    • Drag Employee ID into the area in the bottom half of the screen called "Rows"
    • Drag Status into "Columns"
    • Drag Date into "Values"
  5. You'll see a table but just with blanks or 1s in it (the number of dates it found per employee of status). To change that (assuming there's only 1 per combination), right click (or down arrow) on Date in "Values", pick Value Field Settings and
    • Under Summarize... pick Max
    • Click Show Values As tab as then under Base Field, click Number Format then Custom and Under Type, type dd-mmm-yyyy (so dates will appear like 10-Nov-2020)
    • Click okay
  6. Now you'll see dates in the pivot table.

It's a matter of tidying up the pivot table now, so please do this

  1. To remove the row and column totals (meaningless in your case), right click somewhere in the table and pick Pivot Table Options... then in the Tables and Filters tab, untick Show grand totals for rows (and same for columns, below that) then click OK
  2. To reorder the columns, click the down arrow on "Column Labels" then clich Sort Z to A (so they go Unassigned, Block, Assigned as you want).
  3. Rename the pivot table heading (currently) Max of Date, Column Labels, Row Labels) by clicking on them then typig something in the formula bar (where you'd add values to a cell) but note that they can't be the same as Field Names (so you could change Max of Date to "Status Date"  but not to Date, which exists).
    • When doing this for real, you might want to change your original field names (e.g. to Date1) before creating the pivot table so in the Pivot they cange be meaningful (so Date would be allowed)

It seems a lot but Pivot Tables are great for loads of data. 

Hope this helps

Discuss

Discussion

I didn't find the field "Column Labels" (or the down arrow in it). However, selecting any or all of the column headers in the table except the first one and then clicking the down arrow on the ribbon's "Sort & Filter" button does enable sorting ascending or descending. The sort will be applied to all columns (except the first one) whatever was selected. I tried switching just two columns but that didn't work. Cut and paste wouldn't work in a table, either. But the table can be converted to a normal range in which cells and columns can be moved.
Variatus (rep: 4889) Dec 9, '20 at 8:20 pm
@Variatus. Thanks for your comments.

To see the field "Column Labels" , you needed to follow the detailed instructions but "...follow them carefully please on your original temp.xlsx file" as I said (since my answer file had the labels already editted and columns sorted).

Pivot table have some contraints (like not being able to swap coulmns by dragging) but fieiled can be filtered and even grouped semi-automatically  (e.g see my answer file under recent forum question Calculating current age of employees through using Pivot Table where I grouped rnages of age and experience).

You can cut (or rather copy) part of all of the pivot table but need to paste it outside the table (and if needs be re-format cells).

Agreed on the option of converting to a range then manipulating but my answer (or rather the revision) was aimed at getting a quick result from re-pointing a simple solution to lots of data.
John_Ru (rep: 6142) Dec 10, '20 at 4:52 am
HHank
You need to be careful since there's an assumption that there's only one set of dates per employee. If not ,the dates will be the last (Max) dates at that status but you could drag Date into the Values area of "PivotTable Fields again and leave it at the default Count- so it will show a count greater than 1 (and you could apply Conditional Formatting to highlight those).
John_Ru (rep: 6142) Dec 10, '20 at 4:53 am
@Don- am I right to say there isn't a TeachExcel tutorial on pivot tables? A quick search didn't reveal one to me.
John_Ru (rep: 6142) Dec 10, '20 at 4:53 am
Hhunk82. How did you go on with the pivot table? 
John_Ru (rep: 6142) Dec 16, '20 at 11:03 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login