VBA to transpose blocks (arrays) of data

Antonf

New Member
Joined
Sep 16, 2009
Messages
21
I have to transform literally hundreds of blocks of data exported from financial software into a different format for my manager.
The source data is on the left in the image and the required output is on the right.
Doing it manually is not that difficult but i will drive me to drink!
Any help will be highly appreciated!
Transpose.xlsx
ABCDEFGHIJKLMNO
1CodeDescriptionMar 2023Apr 2023May 2023Jun 2023400190459150
2NR0001Name1NR0001Name1
340017.917.917.917.91Mar 20237.91177.12178.73
49045177.12177.12177.12177.12Apr 20237.91177.12182.54
59150178.73182.54229.54191.93May 20237.91177.12229.54
6NR0004Name2Jun 20237.91177.12191.93
740017.917.917.917.91NR0004Name2
89045177.12177.12177.12177.12Mar 20237.91177.12551.67
99150551.67514.18664.42636.36Apr 20237.91177.12514.18
10May 20237.91177.12664.42
11Jun 20237.91177.12636.36
Sheet1
 

Attachments

  • Transpose.jpeg
    Transpose.jpeg
    68.5 KB · Views: 12

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Think of using Power query?
Book1
ABCDEFGHIJKLMNOP
1CodeDescriptionMar-23Apr-23May-23Jun-23400190459150
2NR0001Name1NR0001Name1
340017.917.917.917.91Mar-237.91177.12178.73
49045177.12177.12177.12177.12Apr-237.91177.12182.54
59150178.73182.54229.54191.93May-237.91177.12229.54
6NR0004Name2Jun-237.91177.12191.93
740017.917.917.917.91NR0004Name2
89045177.12177.12177.12177.12Mar-237.91177.12551.67
99150551.67514.18664.42636.36Apr-237.91177.12514.18
10May-237.91177.12664.42
11Jun-237.91177.12636.36
12
13NRDescriptionAttribute400190459150
14NR0001Name1Apr-237.91177.12182.54
15NR0001Name1Jun-237.91177.12191.93
16NR0001Name1Mar-237.91177.12178.73
17NR0001Name1May-237.91177.12229.54
18NR0004Name2Apr-237.91177.12514.18
19NR0004Name2Jun-237.91177.12636.36
20NR0004Name2Mar-237.91177.12551.67
21NR0004Name2May-237.91177.12664.42
Sheet1


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Code", type any}, {"Description", type text}, {"Mar-23", type number}, {"Apr-23", type number}, {"May-23", type number}, {"Jun-23", type number}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "NR", each if [Code]= Text.Select([Code], {"0".."9"} ) then null else [Code]),
    #"Replaced Errors" = Table.ReplaceErrorValues(#"Added Custom", {{"NR", null}}),
    #"Filled Down" = Table.FillDown(#"Replaced Errors",{"NR"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Filled Down",{"Code", "NR", "Description", "Mar-23", "Apr-23", "May-23", "Jun-23"}),
    #"Filled Down1" = Table.FillDown(#"Reordered Columns",{"Description"}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Filled Down1", {"Code", "NR", "Description"}, "Attribute", "Value"),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Unpivoted Columns", {{"Code", type text}}, "en-IE"), List.Distinct(Table.TransformColumnTypes(#"Unpivoted Columns", {{"Code", type text}}, "en-IE")[Code]), "Code", "Value", List.Sum)
in
    #"Pivoted Column"
 
Upvote 1
Solution
Thank you. Power Query is a mystery to me, but I'm more than prepared to learn. Will revert soonest.
 
Upvote 0
Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

- Follow this link to learn how to install Power Query in Excel 2010 / 2013.

- Follow this link for an introduction to Power Query functionality.

- Follow this link for a video which demonstrates how to use Power Query code provided.
 
Upvote 0
Hi, thanks, but I have NO idea how to use this. I am OK with "normal" Excel stuff, but this is beyond me...

Please ELI5
 
Upvote 0
Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

- Follow this link to learn how to install Power Query in Excel 2010 / 2013.

- Follow this link for an introduction to Power Query functionality.

- Follow this link for a video which demonstrates how to use Power Query code provided.
Thanks a lot, the links really helped!!!
 
Upvote 0
Think of using Power query?
Book1
ABCDEFGHIJKLMNOP
1CodeDescriptionMar-23Apr-23May-23Jun-23400190459150
2NR0001Name1NR0001Name1
340017.917.917.917.91Mar-237.91177.12178.73
49045177.12177.12177.12177.12Apr-237.91177.12182.54
59150178.73182.54229.54191.93May-237.91177.12229.54
6NR0004Name2Jun-237.91177.12191.93
740017.917.917.917.91NR0004Name2
89045177.12177.12177.12177.12Mar-237.91177.12551.67
99150551.67514.18664.42636.36Apr-237.91177.12514.18
10May-237.91177.12664.42
11Jun-237.91177.12636.36
12
13NRDescriptionAttribute400190459150
14NR0001Name1Apr-237.91177.12182.54
15NR0001Name1Jun-237.91177.12191.93
16NR0001Name1Mar-237.91177.12178.73
17NR0001Name1May-237.91177.12229.54
18NR0004Name2Apr-237.91177.12514.18
19NR0004Name2Jun-237.91177.12636.36
20NR0004Name2Mar-237.91177.12551.67
21NR0004Name2May-237.91177.12664.42
Sheet1


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Code", type any}, {"Description", type text}, {"Mar-23", type number}, {"Apr-23", type number}, {"May-23", type number}, {"Jun-23", type number}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "NR", each if [Code]= Text.Select([Code], {"0".."9"} ) then null else [Code]),
    #"Replaced Errors" = Table.ReplaceErrorValues(#"Added Custom", {{"NR", null}}),
    #"Filled Down" = Table.FillDown(#"Replaced Errors",{"NR"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Filled Down",{"Code", "NR", "Description", "Mar-23", "Apr-23", "May-23", "Jun-23"}),
    #"Filled Down1" = Table.FillDown(#"Reordered Columns",{"Description"}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Filled Down1", {"Code", "NR", "Description"}, "Attribute", "Value"),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Unpivoted Columns", {{"Code", type text}}, "en-IE"), List.Distinct(Table.TransformColumnTypes(#"Unpivoted Columns", {{"Code", type text}}, "en-IE")[Code]), "Code", "Value", List.Sum)
in
    #"Pivoted Column"
Thanks, it seems I can do a LOT of work a LOT more efficient using Power Query!
 
Upvote 0
Thanks, it seems I can do a LOT of work a LOT more efficient using Power Query!
yes its great for handling large or even small data sets and once you get used to the steps process you can insert a step delete a step so a lot of flexibility
 
Upvote 1
Dear kind people
I have to go through the same exercise again, for one month only. Using the same output for some reason Power Query can'r find the column Code... I've even copied the heading from the previous sheet - no go!
Transpose2.xlsx
ABC
3CodeDescriptionJul-23
4AM002Name1
580012018.47
69045177.12
79150191.93
8AM004Mr Robin Shawn Van Biljon
9800113760.3
109045177.12
119150567.7
Sheet1


The code I'm using:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Code", type any}, {"Description", type text}, {"Jul-23", type number}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "NR", each if
Code:
= Text.Select([Code], {"0".."9"} ) then null else [Code]),
    #"Replaced Errors" = Table.ReplaceErrorValues(#"Added Custom", {{"NR", null}}),
    #"Filled Down" = Table.FillDown(#"Replaced Errors",{"NR"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Filled Down",{"Code", "NR", "Description", "Jul-23"}),
    #"Filled Down1" = Table.FillDown(#"Reordered Columns",{"Description"}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Filled Down1", {"Code", "NR", "Description"}, "Attribute", "Value"),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Unpivoted Columns", {{"Code", type text}}, "en-IE"), List.Distinct(Table.TransformColumnTypes(#"Unpivoted Columns", {{"Code", type text}}, "en-IE")[Code]), "Code", "Value", List.Sum)
in
    #"Pivoted Column"
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,594
Members
452,656
Latest member
earth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top