Transpose every 3 rows to 3 columns

praveenpatel421983

New Member
Joined
Aug 17, 2017
Messages
41
Hi,
I have a excel which has 1000 of rows but every 3 rows belongs to one category. I want to transpose into 3 columns. I searched through various data but couldn't find the right looping. Please help. Number of columns for each category is not same.

Category 1Day 1abcdefgh
Day 2ijklmnop
Day 3qrstuvwx
Category 2Day 1abcdef
Day 2ghijkl
Day 3mnopqr
Category 3Day 1abcdefg
Day 2hijklmn
Day 3opqrstu
Category 4Day 1abcde
Day 2fghij
Day 3klmno

<tbody>
</tbody>

It should come like below format

Day 1Day 2Day3
Category 1aiq
bjr
cks
dlt
emu
fnv
gow
hpx
Category 2agm
bhn
cio
djp
ekq
flr
Category 3aho
bip
cjq
dkr
els
fmt
gnu
Category 4afk
bgl
chm
din
ejo

<tbody>
</tbody>

Can anyone please help?

thanks
Praveen Patelappa
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
With your data beginning A1, try this:

Code:
Sub trnsp()
Const groupNum As Long = 3
Dim k As Long, vDays As Variant, vValues As Variant, lCol As Long

Application.ScreenUpdating = False

For k = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -groupNum
    lCol = Rows(k).Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious).Column
    
    vDays = Range("A" & k).Offset(, 1).Resize(groupNum).Value
    vValues = Range("A" & k).Offset(, 2).Resize(groupNum, lCol - 2)
    
    Range("A" & k).Offset(0, 1).Resize(groupNum, lCol - 1).ClearContents
    
    If k = 1 Then Range("A" & k).Offset(, 1).Resize(, groupNum).Value = Application.Transpose(vDays)
    
    If UBound(vValues, 2) > (groupNum + (k = 1)) Then
        Range("A" & k).Offset(1).Resize(UBound(vValues, 2) - (groupNum + (k = 1))).EntireRow.Insert
    Else
        If (groupNum + (k = 1) - UBound(vValues, 2)) > 0 Then Range("A" & k).Offset(1).Resize(groupNum + (k = 1) - UBound(vValues, 2)).EntireRow.Delete
        '   Stop
    End If
    
    Range("A" & k).Offset(-(k = 1), 1).Resize(UBound(vValues, 2), groupNum).Value = Application.Transpose(vValues)
Next k

MsgBox "Done"
End Sub
 
Upvote 0
VBA Geek, You are my man!

It worked perfectly. Thanks a lot for that!

One more help. Small explanation of each line would help me enhance my knowledge and reduce asking this level of question. Please take some time and add some explanation....please.

Sorry for the trouble.

Thanks again
Praveen
 
Upvote 0
VBA Geek, You are my man!

It worked perfectly. Thanks a lot for that!

One more help. Small explanation of each line would help me enhance my knowledge and reduce asking this level of question. Please take some time and add some explanation....please.

Sorry for the trouble.

Thanks again
Praveen



yes sure, i added some comments!

Code:
[FONT=comic sans ms]Sub trnsp()
Const groupNum As Long = 3 ' number of days t o be transposed, you can also change to 4, 5 6 etc if necessary
Dim k As Long, vDays As Variant, vValues As Variant, lCol As Long

Application.ScreenUpdating = False

For k = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -groupNum ' loop backwards from last row until row 1  stepping each groupNum

    lCol = Rows(k).Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious).Column ' find number of columns for this category
    
    vDays = Range("A" & k).Offset(, 1).Resize(groupNum).Value ' store in array the days
    vValues = Range("A" & k).Offset(, 2).Resize(groupNum, lCol - 2) ' store in array the data
    
    Range("A" & k).Offset(0, 1).Resize(groupNum, lCol - 1).ClearContents ' clear content of the category we are in
    
    If k = 1 Then Range("A" & k).Offset(, 1).Resize(, groupNum).Value = Application.Transpose(vDays) ' if we are in first row paste transpose the days
    
    ' determine the number of rows that need to be inserted
    If UBound(vValues, 2) > (groupNum + (k = 1)) Then
        Range("A" & k).Offset(1).Resize(UBound(vValues, 2) - (groupNum + (k = 1))).EntireRow.Insert
    Else
        If (groupNum + (k = 1) - UBound(vValues, 2)) > 0 Then Range("A" & k).Offset(1).Resize(groupNum + (k = 1) - UBound(vValues, 2)).EntireRow.Delete
    End If
    
    ' paste transpose the data of this category
    Range("A" & k).Offset(-(k = 1), 1).Resize(UBound(vValues, 2), groupNum).Value = Application.Transpose(vValues)
Next k

MsgBox "Done"
End Sub[/FONT]
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,536
Members
449,037
Latest member
tmmotairi

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