Transpose data with breakdown at every 28th row

beautify5

New Member
Joined
Oct 27, 2014
Messages
19
Hello Experts,

I have been trying to fit my head to find a solution but sadly haven't been able to.

I have huge data with 2 columns, there are 28 fields for each of the person dumped up in a single column. 1st column has the header data which obviously is repeated after every 28th rows and 2nd column has details about the persons.

I want to have the data from column 1 to be transposed as 28 columns and under this column/header there should be values from column 2 transposed to rows breakdown at every 28 cells. so 1st 28 values of the column 2 as row 1 and then next 28 values of the column 2 as row 2 and so on..

This might be a little confusing but i am sharing the demo work file, to make some sense. thanks a lot in advance!!

COLUM1 COLUMN2
asIDAA1
asNameAA2
asAddrAA3
asCityAA4
asSTAA5
asZIPAA6
fileAA7
aseNameAA8
aseAddrAA9
aseCityAA10
aseSTAA11
aseZIPAA12
CtrlNoAA13
WagesAA14
FedWHAA15
SSWagesAA16
SSWhAA17
midWageAA18
midWHAA19
RetPlanAA20
CODE12AA21
AMNT12AA22
CODE12AA23
AMNT12AA24
StateCDAA25
StateIDAA26
StatWageAA27
StateWHAA28
asIDBB1
asNameBB2
asAddrBB3
asCityBB4
asSTBB5
asZIPBB6
fileBB7
aseNameBB8
aseAddrBB9
aseCityBB10
aseSTBB11
aseZIPBB12
CtrlNoBB13
WagesBB14
FedWHBB15
SSWagesBB16
SSWhBB17
midWageBB18
midWHBB19
RetPlanBB20
CODE12BB21
AMNT12BB22
CODE12BB23
AMNT12BB24
StateCDBB25
StateIDBB26
StatWageBB27
StateWHBB28

<colgroup><col width="68" span="2" style="width:51pt"> </colgroup><tbody>
</tbody>


After macro

asIDasNameasAddrasCityasSTasZIPfileaseNameaseAddraseCityaseSTaseZIPCtrlNoWagesFedWHSSWagesSSWhmidWagemidWHRetPlanCODE12AMNT12CODE12AMNT12StateCDStateIDStatWageStateWH
AA1AA2AA3AA4AA5AA6AA7AA8AA9AA10AA11AA12AA13AA14AA15AA16AA17AA18AA19AA20AA21AA22AA23AA24AA25AA26AA27AA28
BB1BB2BB3BB4BB5BB6BB7BB8BB9BB10BB11BB12BB13BB14BB15BB16BB17BB18BB19BB20BB21BB22BB23BB24BB25BB26BB27BB28

<colgroup><col width="68" span="28" style="width:51pt"> </colgroup><tbody>
</tbody>
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hope this helps.
Code:
Sub trp()
Dim i As Long, j As Long
Dim rng As Range
j = 2
Range("D1:AE1").Value = WorksheetFunction.Transpose(Range("A1:A28"))
For i = 1 To cells(Rows.count, 2).End(xlUp).row Step 28
    Range(cells(j, 4), cells(j, 31)).Value = WorksheetFunction.Transpose(Range(cells(i, 2), cells(i + 27, 2)))
    j = j + 1
Next
End Sub
 
Upvote 0
Thank you, this is awesome!

I am sorry but i just realized there is inconsistency with the data. so for few individuals there aren't all the 28 fields, so some are having 24 or 26. Can you please do me a favor and suggest a way with 28 row not being trigger to break down but Column 1 value "asID" as the trigger for having the data breakdown for each separate row. I hope i am not sounding confused.

In simple words result should have each row starting with value corresponding for ASID and similarly have the respective values of the 28 header populated and have the values blank for the respective row header which isn't present. example as below:

asIDAA1
asNameAA2
asAddrAA3
asCityAA4
asSTAA5
asZIPAA6
fileAA7
aseNameAA8
aseAddrAA9

<tbody>
</tbody>

asIDBB1
asNameBB2
asCityBB4
asSTBB5
fileBB7
aseNameBB8

<tbody>
</tbody>


Result:

asIDasNameasAddrasCityasSTasZIPfileaseNameaseAddr

<tbody>
</tbody>
AA1AA2AA3AA4AA5AA6AA7AA8AA9

<tbody>
</tbody>
BB1BB2BB4BB5BB7BB8BB9

<tbody>
</tbody>

Hope this helps.
Code:
Sub trp()
Dim i As Long, j As Long
Dim rng As Range
j = 2
Range("D1:AE1").Value = WorksheetFunction.Transpose(Range("A1:A28"))
For i = 1 To cells(Rows.count, 2).End(xlUp).row Step 28
    Range(cells(j, 4), cells(j, 31)).Value = WorksheetFunction.Transpose(Range(cells(i, 2), cells(i + 27, 2)))
    j = j + 1
Next
End Sub
 
Upvote 0
Does the first column have blanks? If the first column has blanks, my code will work almost correctly because "asID" shows up every 28 rows.
If the first column does not have blanks, I know my code will not work correctly.
Or does the blanks may or may not appear with regularity like your second sample? Between AA9 and BB1 has just one blank row.
 
Upvote 0
The above code didn't work well. So, actually there are no blank cells but they are missing. so in my example i know i left blank cells but what i meant was the cells arent even present there. However, AsID is always there and the AA1,BB1,CC1 values would always be there.
 
Upvote 0
In data you posted in post#1 some of the headers are repeated (code12, amnt12) is this actually the case in your real data?
If not are you happy to have the headers in another sheet that can be copied over?
 
Upvote 0
If the answer to the 1st question above is no & to the 2nd is yes, try
Code:
Sub CopyTransposeWithBlanks()

    Dim Ary As Variant
    Dim Ar As Areas
    Dim Rng As Range
    Dim Cl As Range
    Dim Rw As Long
    Dim Col As Long
    
Application.ScreenUpdating = False

    Ary = Sheets("[COLOR=#ff0000]Master[/COLOR]").Range("A1:AB1").Value
    With Sheets("[COLOR=#ff0000]Test1[/COLOR]")
        With .Range("A1", .Range("A" & Rows.Count).End(xlDown))
            .Replace "asID", True, xlWhole, , True, , False, False
            .SpecialCells(xlConstants, xlLogical).EntireRow.Insert
            .Replace True, "asID", xlWhole, , True, , False, False
            Set Ar = .SpecialCells(xlConstants).Areas
        End With
        
        .Range("C1").Resize(, 28).Value = Ary
        Rw = 2
        For Each Rng In Ar
            For Each Cl In Rng
                Col = WorksheetFunction.Match(Cl, Ary, 0)
                Cl.Offset(, 1).copy .Cells(Rw, Col + 2)
            Next Cl
            Rw = Rw + 1
        Next Rng
    End With

End Sub
changing the sheet names in red to suit
 
Upvote 0
This will deal with the two duplicated headers, but if one is missing the data may go into the wrong column.
Code:
Sub CopyTransposeWithBlanks()

    Dim Ary As Variant
    Dim Ar As Areas
    Dim Rng As Range
    Dim Cl As Range
    Dim Rw As Long
    Dim Col As Long
    
Application.ScreenUpdating = False

    Ary = Sheets("[COLOR=#ff0000]Master[/COLOR]").Range("A1:AB1").Value
    With Sheets("[COLOR=#ff0000]Test1[/COLOR]")
        With .Range("A1", .Range("A" & Rows.Count).End(xlDown))
            .Replace "asID", True, xlWhole, , True, , False, False
            .SpecialCells(xlConstants, xlLogical).EntireRow.Insert
            .Replace True, "asID", xlWhole, , True, , False, False
            Set Ar = .SpecialCells(xlConstants).Areas
        End With
        
        .Range("C1").Resize(, 28).Value = Ary
        Rw = 2
        For Each Rng In Ar
            For Each Cl In Rng
                Col = WorksheetFunction.Match(Cl, Ary, 0)
                If Col = 21 And .Cells(Rw, Col + 2) <> "" Then
                    Col = 23
                ElseIf Col = 22 And .Cells(Rw, Col + 2) <> "" Then
                    Col = 24
                End If
                Cl.Offset(, 1).copy .Cells(Rw, Col + 2)
            Next Cl
            Rw = Rw + 1
        Next Rng
    End With

End Sub
Once again, change sheet names to suit
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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