Rearraging data in excel - macro?

excelhelp1235

New Member
Joined
Mar 11, 2016
Messages
1
Hi,

I need help rearranging a large amount of data in Excel.

The current format is like this (Sample of the data)

Company nameLeverage 2012Leverage 2011Leverage 2010Leverage 2009Leverage 2008Leverage 2007Leverage 2006Leverage 2005Levergae 2004Leverage 2003Market CapMarket Cap.Market Cap.Market Cap.Market Cap.Market Cap.Market Cap.Market Cap.Market Cap.
%%%%%%%%%%th USDth USDth USDth USDth USDth USDth USDth USDth USD
2012201120102009200820072006200520042003201220112010200920082007200520062004
3M COMPANY63.6574.1761.7679.93117.6772.2465.4557.5361.2274.0063,796,45957,280,03861,692,34158,526,90939,872,63360,139,46760,579,27060,915,38764,151,493
ACCENTURE PLC106.38101.84121.03111.49118.7484.5791.44143.15145.03297.1639,103,72234,534,62123,324,08220,270,84324,865,69125,957,02914,028,41417,081,69810,297,406
ADVANCE AUTO PARTS, INC.69.7273.2245.0025.4248.8254.3552.2455.8576.1782.395,307,8905,044,4065,560,1053,830,6723,232,7606,710,0164,669,8863,768,4713,268,574
AIR PRODUCTS & CHEMICALS INC121.18107.41106.32129.09106.5688.0982.9789.2693.06113.3217,508,12216,276,96517,602,26216,298,13814,508,78524,386,77812,643,87815,218,97312,469,606
AIRGAS INC229.22151.44152.23124.47153.80148.58161.76124.95141.41145.597,527,9566,794,1215,591,7555,263,2592,749,4773,750,5773,021,3453,357,6711,618,977
ALCOA INC162.91149.58151.80171.55160.7599.60110.6199.6098.42124.869,263,3139,206,22715,719,98615,706,9729,011,57430,999,81725,728,88626,111,73127,336,720
ALTRIA GROUP, INC.800.79713.02488.64624.01614.57118.59100.64138.67159.90204.8363,669,33660,972,92951,402,31240,677,50631,029,590159,195,666151,940,059174,511,451124,244,342
AMEREN CORP200.77178.31181.73182.52186.86159.78163.91155.09167.30191.357,453,7377,969,2566,717,2935,786,2566,885,54111,222,6467,030,89711,082,4576,879,960
AMERICAN ELECTRIC POWER COMPANY INC226.28220.82230.70239.08265.49254.54256.80248.29261.37310.5620,710,43219,970,19917,299,44116,631,51313,514,05118,643,86811,951,69616,843,49911,065,550
AMETEK INC82.6686.6389.5785.55103.9588.6887.6189.1281.5595.549,128,9206,740,3006,281,9514,123,7473,224,1365,025,0962,840,0533,370,4202,381,398
AMGEN INCORPORATED154.99127.0364.5757.7455.8170.4651.0425.6833.1522.0166,146,02156,282,90751,870,36757,256,67161,187,33450,510,087102,657,81588,924,10683,508,736
AMPHENOL CORP82.1975.1244.8355.5774.8770.4793.91133.83117.20201.3010,374,1457,522,9049,216,0017,919,7374,215,2148,266,3883,872,8385,432,1243,214,824
ANADARKO PETROLEUM CORP135.57159.87130.79132.33138.68172.63300.5283.3897.82118.9937,137,10738,010,16637,744,32430,680,14317,695,97830,635,05823,569,82120,465,28016,122,005
ARCHER-DANIELS-MIDLAND COMPANY50.1655.8559.6266.9168.0353.8754.8359.5366.0070.6918,038,86319,232,08816,603,54717,186,32721,732,39222,234,47314,189,11427,396,00811,136,266
AT&T INC.164.19130.26115.93134.07141.27109.17103.02127.17125.28126.97191,472,795179,202,239173,635,795165,405,034167,950,500252,051,38495,836,422226,303,86485,566,710
AUTONATION INC138.7497.8772.3554.2063.4061.2550.8416.7824.3927.344,833,9915,158,0464,173,5143,341,1871,747,3102,880,8655,596,9697,225,8336,387,863
AVERY DENNISON CORPORATION124.62114.21121.71169.39165.30145.2082.32101.27107.69120.573,477,7903,045,7344,632,7304,114,0533,478,7275,658,3896,425,5807,897,4066,971,992
BAKER HUGHES INC38.5236.9843.1135.1045.5630.9035.1437.4445.4862.4717,958,56421,229,70024,655,91712,544,2139,862,51225,804,84819,887,45924,559,70713,961,794
BEST BUY CO, INC113.1392.0745.8248.0960.3534.3017.5318.4321.5332.065,449,6219,543,86312,708,89315,258,16811,915,02418,042,00626,481,83122,274,84917,151,621
BOSTON SCIENTIFIC CORP123.9671.6577.2680.1381.4886.8184.7256.8950.7350.497,867,1977,911,24111,506,98013,593,87011,621,79317,337,83920,161,58814,143,57329,266,821
BROWN FORMAN CORP48.6758.2549.8753.7440.5862.6238.1353.5184.98109.057,410,7096,367,7525,252,7204,352,8054,347,7844,236,2166,659,9974,877,1405,623,197
CA, INC.52.1153.0555.8566.8780.19111.35105.8351.2662.4674.6511,480,15813,384,36312,333,08212,205,0239,137,25611,551,31815,756,82113,663,87815,693,122
CAMERON INTERNATIONAL CORPORATION44.0642.1635.4939.0768.5845.4368.2436.9049.4451.8213,929,48012,061,42612,305,4649,225,2064,500,10510,527,2214,695,7546,017,1432,721,064
CARDINAL HEALTH INC69.1567.6164.6351.4170.6358.7441.9242.4861.1648.3314,535,18615,923,63812,169,24011,009,97718,406,99134,357,51726,188,82429,258,89331,860,493
CARMAX INC211.46194.55201.3513.8428.4629.1422.0224.9420.3518.328,763,9836,953,9207,977,8214,499,5192,078,3704,009,2153,236,0095,427,6783,398,736
CARNIVAL CORPORATION40.2341.9643.6748.9453.0447.5746.2346.5153.9657.0922,982,79019,767,84125,788,54419,995,32813,110,14328,154,80433,397,08530,026,11932,489,987
CATERPILLAR INC280.21354.25324.29435.06682.80339.25426.14288.05311.24349.9058,697,91158,666,52059,832,13835,602,94926,870,19446,145,27439,479,44140,845,78133,558,379
CENTURYLINK, INC.162.07152.11118.09125.72147.10126.68118.70101.62115.70115.5224,421,20622,974,94514,000,89510,773,3862,736,7834,576,4724,626,1526,091,0074,948,420
CERNER CORP12.3612.5812.4114.8519.7525.0530.6040.9934.7241.0913,297,93710,376,1067,849,4466,729,7213,173,6064,509,5083,165,0323,478,3841,851,113
CINTAS CORP73.7070.2041.5444.4952.7246.3144.4528.8232.1640.094,789,4524,773,2143,974,6163,558,4834,536,7406,085,9216,724,9967,056,4987,559,582
CMS ENERGY CORP397.03401.02413.61392.64398.42421.37442.65448.80515.27576.036,465,6675,598,6214,549,1083,595,6452,286,9593,912,0821,587,2923,714,6651,143,157
COACH INC20.8526.6928.8024.1220.556.888.094.6111.766.8716,828,66219,374,92310,913,6088,536,6539,856,70617,588,06512,676,12911,297,0578,372,064
COCA-COLA COMPANY (THE)82.7782.4979.6238.0936.7538.7824.7419.9636.7940.38162,587,105158,918,120152,720,150132,079,344104,734,646141,824,63999,607,827113,023,019102,894,312
COLGATE PALMOLIVE CO352.63293.85199.25152.28270.27210.01356.84353.36413.37502.8449,393,23944,716,69538,793,27740,844,25234,593,15039,741,03131,953,96433,691,24029,804,281
COMCAST CORPORATION205.20208.72152.74149.62162.93158.73153.11145.07140.35146.4379,162,35549,683,74645,517,90434,783,41534,789,38737,498,09659,413,25297,027,89376,283,678
CONAGRA FOODS, INC.108.46106.58102.32101.7288.22100.15102.72116.50141.98144.4410,446,39710,398,10110,773,6048,314,02811,498,17714,441,26714,047,49212,140,47215,105,755
CONSOLIDATED EDISON, INC.219.91215.54200.01202.38210.51171.50194.52186.44184.92189.6516,266,16118,168,87514,401,87412,515,59610,652,40213,263,54710,794,89011,200,31010,193,750
CONSTELLATION BRANDS, INC.144.30133.20155.52168.17263.95209.62138.90112.31142.24101.727,084,4043,845,7613,776,1232,980,8902,545,3463,685,0635,077,0355,131,4034,684,945
CORNING INC27.9722.3023.3827.5828.5634.0148.5861.38109.9270.5518,650,35920,399,09630,194,82030,048,16514,812,74038,177,16416,050,16829,268,0899,608,875
CSX CORP216.36222.80201.83187.13200.73172.26159.44178.97226.99207.2620,349,08622,112,01024,176,09319,035,20212,808,43318,490,33111,019,18315,038,7998,699,012
CVS HEALTH CORPORATION38.3138.3538.4743.8339.1940.4940.2932.5339.0922.5160,277,58553,080,48247,245,47745,433,58941,277,19258,706,41721,139,43524,732,01817,216,740
DANAHER CORP51.4053.0738.1245.1351.0064.0256.7539.3345.3951.4938,721,25732,285,77830,847,02124,156,86018,093,09527,234,24215,106,11622,260,81315,547,547

<colgroup><col><col span="19"></colgroup><tbody>
</tbody>



And i need to rearrange it so its in the format:

CompanyYearLeverageMarket CapROAP/B RatioFixed Assets
3M COMPANY200374.0063,796,459etcetcetc
3M COMPANY200461.2257,280,038
3M COMPANY200557.5361,692,341
3M COMPANY200665.4558,526,909
3M COMPANY200772.2439,872,633
3M COMPANY2008117.6760,139,467
3M COMPANY200979.9360,579,270
3M COMPANY201061.7660,915,387
3M COMPANY201174.1764,151,493
3M COMPANY201263.6566,465,229
ACCENTURE PLC2003106.38
ACCENTURE PLC2004101.84
ACCENTURE PLC2005121.03
ACCENTURE PLC2006111.49
ACCENTURE PLC2007118.74
ACCENTURE PLC200884.57
ACCENTURE PLC200991.44
ACCENTURE PLC2010143.15
ACCENTURE PLC2011145.03
ACCENTURE PLC2012297.16

<colgroup><col><col span="6"></colgroup><tbody>
</tbody>
etc
etc

Does anyone have an idea of a quick way to do this?

Thanks for any help
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Welcome to the Forum!

Here's a VBA approach. It could also be done using relatively simple formulae if you'd prefer.

I've assumed your data (excluding headers) is in A6:U47 (including a 2003 column for market cap, which wasn't included in the post) and that the column progression is always 2012 ---> 2003 (i.e. 2005 and 2006 headers for market cap were just accidentally transposed?)

I suggest you test on the data you've posted (works for me!) and then try to tweak the code as indicated for your full dataset.

Code:
Sub Rearrange()

    Dim vIn As Variant, vOut As Variant
    Dim lNoYears As Long, lOutputFields As Long, i As Long, j As Long
    
    lNoYears = 10 '2003 to 2012 inclusive
    lOutputFields = 4   'expand as required
    vIn = Range("A6:U47").Value     'Data excluding headers, expand columns as required
    ReDim vOut(1 To UBound(vIn) * lNoYears, 1 To lOutputFields)
    
    For i = 1 To UBound(vIn)
        For j = 1 To lNoYears
            vOut(10 * (i - 1) + j, 1) = vIn(i, 1)
            vOut(10 * (i - 1) + j, 2) = 2002 + j
            vOut(10 * (i - 1) + j, 3) = vIn(i, 12 - j) 'Leverage
            vOut(10 * (i - 1) + j, 4) = vIn(i, 22 - j) 'Market Cap
            'vOut(10 * (i - 1) + j, 5) = vIn(i, 32 - j) 'Next field
            'etc
        Next j
    Next i
    
    Worksheets("Sheet2").Range("A1").Resize(UBound(vOut), UBound(vOut, 2)).Value = vOut

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,509
Members
448,967
Latest member
screechyboy79

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