Arrannge data in a excel table

murari28

New Member
Joined
Jun 24, 2020
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hello Experts

I have data in excel which comes like product model is written at the top of a column and product serial numbers below it, The second column's header is color and color names below it. This kind of data are for multiple number of models hence can't predict the number of data columns. I need the to be stacked model wise to simplify in three columns 1st as model name, 2nd as serial number and 3rd as Color.

The data Comes inNeed Like this
Model1ColorModel2ColorModel3ColorMOdel4ColorModelSerial No.Color
1Black21Black41Green61BlackModel1445635011569378Black
2Black22Black42Green62BlackModel1695442316016730Black
3Black23Black43Green63BlackModel1872194635375407Black
4Black24Black44Green64BlackModel1888543778254774Black
5Black25Black45Green65BlackModel1419002119059547Black
6Black26Black46Green66BlackModel1898705496355513Black
7Black27Black47Green67BlackModel1387359515924780Black
8Black28Black48Green68BlackModel1838179760844080Black
9Black29Black49Green69BlackModel1587738572745312Black
10Black30Black50Green70BlackModel1467907164619356Black
11Green31Blue51Black71GoldModel1902393463496469Green
12Green32Blue52Black72GoldModel1783556675137366Green
13Green33Blue53Black73GoldModel1747932730314419Green
14Green34Blue54Black74GoldModel1470800692415356Green
15Green35Blue55Black75GoldModel1451979767688421Green
16Green36Blue56Black76GoldModel1362567558649947Green
17Green37Blue57Black77GoldModel1902479050803404Green
18Green38Blue58Black78GoldModel1547605102883581Green
19Green39Blue59Black79GoldModel1520003026154996Green
20Green40Blue60Black80GoldModel1529906484901096Green
Model2869013354652431Black
Model2460297059261934Black
Model2638845026339707Black
Model2534138346561508Black
Model2640467948443007Black
Model2342765474980329Black
Model2794954573800445Black
Model2391917911568596Black
Model2628736548741769Black
Model2857939787627368Black
Model2546352082692704Blue
Model2828384838590137Blue
Model2703976970494554Blue
Model2752220122178935Blue
Model2807887096411352Blue
Model2551608197635321Blue
Model2673480160758903Blue
Model2707083664935778Blue
Model2670510076297621Blue
Model2369760067514220Blue


Request experts to please help me.

Thanks in advance
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Make sure your data starts in A1.

VBA Code:
Sub jec()
  ar = Sheets(1).Cells(1).CurrentRegion
  ReDim jv((UBound(ar) - 1) * UBound(ar, 2) / 2, 3)
           
   For i = 1 To UBound(jv)
      x = (i - 1) Mod (UBound(ar) - 1) + 1
      y = 2 * ((i - 1) \ (UBound(ar) - 1)) + 1
      
      jv(i - 1, 0) = ar(1, y)
      jv(i - 1, 1) = ar(x + 1, y)
      jv(i - 1, 2) = ar(x, y + 1)
   Next
          
   Sheets(1).Cells(1, 15).Resize(UBound(jv), 3) = jv
End Sub
 
Upvote 0
Solution
With Power Query which is now called Get and Transform Data in your version and is found on the Data Tab, here is the Mcode to unpivot your data

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Model1", Int64.Type}, {"Color", type text}, {"Model2", Int64.Type}, {"Color2", type text}, {"Model3", Int64.Type}, {"Color3", type text}, {"MOdel4", Int64.Type}, {"Color4", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Color", each if Text.StartsWith([Attribute],"Color") then [Value] else null),
    #"Filled Up" = Table.FillUp(#"Added Custom",{"Color"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Up", each not Text.StartsWith([Attribute], "Color")),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Attribute", Order.Ascending}}),
    #"Renamed Columns" = Table.RenameColumns(#"Sorted Rows",{{"Value", "Serial"}})
in
    #"Renamed Columns"
 
Upvote 0
With Power Query which is now called Get and Transform Data in your version and is found on the Data Tab, here is the Mcode to unpivot your data

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Model1", Int64.Type}, {"Color", type text}, {"Model2", Int64.Type}, {"Color2", type text}, {"Model3", Int64.Type}, {"Color3", type text}, {"MOdel4", Int64.Type}, {"Color4", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Columns", "Color", each if Text.StartsWith([Attribute],"Color") then [Value] else null),
    #"Filled Up" = Table.FillUp(#"Added Custom",{"Color"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Up", each not Text.StartsWith([Attribute], "Color")),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Attribute", Order.Ascending}}),
    #"Renamed Columns" = Table.RenameColumns(#"Sorted Rows",{{"Value", "Serial"}})
in
    #"Renamed Columns"
Thank you very very much for your help

My problem is solved

Thank you very much again........
 
Upvote 0
This kind of data are for multiple number of models hence can't predict the number of data columns.
If you haven't already, just make sure you delete the Changed type step, it is the only place where Models1 to 4 are currently hard coded.
 
Upvote 0
Make sure your data starts in A1.

VBA Code:
Sub jec()
  ar = Sheets(1).Cells(1).CurrentRegion
  ReDim jv((UBound(ar) - 1) * UBound(ar, 2) / 2, 3)
          
   For i = 1 To UBound(jv)
      x = (i - 1) Mod (UBound(ar) - 1) + 1
      y = 2 * ((i - 1) \ (UBound(ar) - 1)) + 1
     
      jv(i - 1, 0) = ar(1, y)
      jv(i - 1, 1) = ar(x + 1, y)
      jv(i - 1, 2) = ar(x, y + 1)
   Next
         
   Sheets(1).Cells(1, 15).Resize(UBound(jv), 3) = jv
End Sub
Thank you very very much for helping me..........
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

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