VBA Reorganize Cohort Data

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I over 500 models of cohort data that I need to reorganize. Please see the data below. I need to take the diagonal data and make it horizontal (row). I need to keep the original data in tact. If possible make the revision starting in column H. Thanks in advance!!!

Grade2015-162016-172017-18
345%34%54%
467%54%47%
578%65%42%
665%84%77%
776%61%85%
878%87%73%

To this:

2015-162016-172017-18
Gr 30.450.540.42
Gr 40.670.650.77
Gr 50.780.840.85
Gr 60.650.610.73
Gr 70.760.87
Gr 80.78
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
with VBA

VBA Code:
Sub cohort()
Range("C2").Delete
Range("D2:D3").Delete
End Sub
 
Upvote 0
What about:

MrExcelFilterMMULT.xlsm
ABCDEFGHI
1Grade2015-162016-172017-182015-162016-172017-18
2345%34%54%Gr 345%0.540.42
3467%54%47%Gr 467%0.650.77
4578%65%42%Gr 578%0.840.85
5665%84%77%Gr 665%0.610.73
6776%61%85%Gr 776%0.870
7878%87%73%Gr 878%00
Sheet1
Cell Formulas
RangeFormula
G1:I1H1=C1
F2:F7F2="Gr "&A2:A7
G2:I7G2=INDEX(B2:D7,{1,2,3},{1,2,3})
Dynamic array formulas.
 
Upvote 0
alansidman,

Thanks that works, but I need to keep the original data state. Is there a way to do what you have given me but have it start in Column H?
 
Upvote 0
kweaver, thank you! that will do the task but was trying to get VBA because of all the data that I have to work with.
 
Upvote 0
kweaver, thank you! that will do the task but was trying to get VBA because of all the data that I have to work with.
I just tried it with 600 rows of data and it was rather instantaneous.

I used 6 columns of dates as well: =INDEX(B2:G601,{1,2,3,4,5,6},{1,2,3,4,5,6})
 
Upvote 0
Here is VBA, sorry if is too late.
VBA Code:
Sub DiagonalToHorizontal()

   Dim vNRows As Long, vNColumns As Integer
   Dim vA1, vA2, vN1 As Long, vN2 As Integer
   
   vNRows = Rows(1).Cells(Rows.Count, "A").End(xlUp).Row
   vNColumns = Columns(1).Cells(1, Columns.Count).End(xlToLeft).Column
   vA1 = Range("A1", Cells(vNRows + vNColumns - 2, vNColumns)).Value
   vA2 = vA1
   For vN1 = 2 To vNRows
      For vN2 = 2 To vNColumns
         vA2(vN1, vN2) = vA1(vN1 + vCounter, vN2)
         vCounter = vCounter + 1
      Next vN2
      vA2(vN1, 1) = "Gr " & vA1(vN1, 1)
      vCounter = 0
  Next vN1
  Range("H1").Resize(vNRows, vNColumns) = vA2
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,457
Members
449,161
Latest member
NHOJ

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