VBA macro to convert partial horizontal data to vertical

MBD

New Member
Joined
Aug 25, 2021
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Hoping someone can help me with this - I've tried searching the forum for the solution, but none quite fits what I'm looking to do.

Essentially, I'm looking to transform excel data as per below illustration:
- create a new column called 'Project ID', which is a combination of Project Number and Stage 1 - 4
- for every stages, repeat Project Number, Project Name, Team and Work Site


sheet1 (original):
Project NumberProject NameTeamWork SiteStage 1Stage 2Stage 3Stage 4
1​
Project 1StudyNew Yorkabcd
2​
Project 2StudyLost Angelesabcd
3​
Project 3MarketingLost Angelesabcd

Into sheet2 (transformed):
Project IDProject NumberProject NameTeamWork Site
1.a
1​
Project 1StudyNew York
1.b
1​
Project 1StudyNew York
1.c
1​
Project 1StudyNew York
2.a
2​
Project 2StudyLost Angeles
2.b
2​
Project 2StudyLost Angeles
2.c
2​
Project 2StudyLost Angeles
3.a
3​
Project 3MarketingLost Angeles
3.b
3​
Project 3MarketingLost Angeles
3.c
3​
Project 3MarketingLost Angeles

Thank you for your help.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You could use Power query to help with this
Book1
ABCDEFGH
1Project NumberProject NameTeamWork SiteStage 1Stage 2Stage 3Stage 4
21Project 1StudyNew Yorkabcd
32Project 2StudyLost Angelesabcd
43Project 3MarketingLost Angelesabcd
5
6Project IDProject NumberProject NameTeamWork Site
71.a1Project 1StudyNew York
81.b1Project 1StudyNew York
91.c1Project 1StudyNew York
101.d1Project 1StudyNew York
112.a2Project 2StudyLost Angeles
122.b2Project 2StudyLost Angeles
132.c2Project 2StudyLost Angeles
142.d2Project 2StudyLost Angeles
153.a3Project 3MarketingLost Angeles
163.b3Project 3MarketingLost Angeles
173.c3Project 3MarketingLost Angeles
183.d3Project 3MarketingLost Angeles
Sheet2


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project Number", type text}, {"Project Name", type text}, {"Team", type text}, {"Work Site", type text}, {"Stage 1", type text}, {"Stage 2", type text}, {"Stage 3", type text}, {"Stage 4", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Project Number", "Project Name", "Team", "Work Site"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Project ID", each [Project Number]&"."&[Value]),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Project ID", "Project Number", "Project Name", "Team", "Work Site", "Attribute", "Value"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Attribute", "Value"})
in
    #"Removed Columns"
1660121675525.png
 
  • Like
Reactions: MBD
Upvote 0
Solution
You could use Power query to help with this
Book1
ABCDEFGH
1Project NumberProject NameTeamWork SiteStage 1Stage 2Stage 3Stage 4
21Project 1StudyNew Yorkabcd
32Project 2StudyLost Angelesabcd
43Project 3MarketingLost Angelesabcd
5
6Project IDProject NumberProject NameTeamWork Site
71.a1Project 1StudyNew York
81.b1Project 1StudyNew York
91.c1Project 1StudyNew York
101.d1Project 1StudyNew York
112.a2Project 2StudyLost Angeles
122.b2Project 2StudyLost Angeles
132.c2Project 2StudyLost Angeles
142.d2Project 2StudyLost Angeles
153.a3Project 3MarketingLost Angeles
163.b3Project 3MarketingLost Angeles
173.c3Project 3MarketingLost Angeles
183.d3Project 3MarketingLost Angeles
Sheet2


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project Number", type text}, {"Project Name", type text}, {"Team", type text}, {"Work Site", type text}, {"Stage 1", type text}, {"Stage 2", type text}, {"Stage 3", type text}, {"Stage 4", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Project Number", "Project Name", "Team", "Work Site"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Project ID", each [Project Number]&"."&[Value]),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Project ID", "Project Number", "Project Name", "Team", "Work Site", "Attribute", "Value"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Attribute", "Value"})
in
    #"Removed Columns"
View attachment 71253
Hi Kerryx, thank you so much - this is brilliant!
Don't know why I didn't think of Power Query!
 
Upvote 0

Forum statistics

Threads
1,223,935
Messages
6,175,493
Members
452,649
Latest member
mr_bhavesh

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