VBA Transpose Cells in a range

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Hi, Is it possible to transpose a group of cells then move to the next group of cells, Can someone help with tis please

Example:

From This

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]This1
[/TD]
[TD]This2
[/TD]
[/TR]
[TR]
[TD]This3
[/TD]
[TD]This4
[/TD]
[/TR]
[TR]
[TD]This5
[/TD]
[TD]This6
[/TD]
[/TR]
[TR]
[TD]That1
[/TD]
[TD]That2
[/TD]
[/TR]
[TR]
[TD]That3
[/TD]
[TD]That4
[/TD]
[/TR]
[TR]
[TD]That5
[/TD]
[TD]That6
[/TD]
[/TR]
[TR]
[TD]Something1
[/TD]
[TD]Something2
[/TD]
[/TR]
[TR]
[TD]Something3
[/TD]
[TD]Something4
[/TD]
[/TR]
[TR]
[TD]Something5
[/TD]
[TD]Something6
[/TD]
[/TR]
[TR]
[TD]Nothing1
[/TD]
[TD]Nothing2
[/TD]
[/TR]
[TR]
[TD]Nothing3
[/TD]
[TD]Nothing4
[/TD]
[/TR]
[TR]
[TD]Nothing5
[/TD]
[TD]Nothing6
[/TD]
[/TR]
</tbody>[/TABLE]


To This

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]This1
[/TD]
[TD]This2
[/TD]
[TD]This3
[/TD]
[TD]This4
[/TD]
[TD]Ths5
[/TD]
[TD]This6
[/TD]
[/TR]
[TR]
[TD]That1
[/TD]
[TD]That2
[/TD]
[TD]That3
[/TD]
[TD]That4
[/TD]
[TD]That5
[/TD]
[TD]That6
[/TD]
[/TR]
[TR]
[TD]Something1
[/TD]
[TD]Something2
[/TD]
[TD]Something3
[/TD]
[TD]Something4
[/TD]
[TD]Something5
[/TD]
[TD]Something6
[/TD]
[/TR]
[TR]
[TD]Nothing1
[/TD]
[TD]Nothing2
[/TD]
[TD]Nothing3
[/TD]
[TD]Nothing4
[/TD]
[TD]Nothing5
[/TD]
[TD]Nothing6
[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Are there always exactly 6 items for each grouping?
If not, are the names to move to the line always EXACTLY the same, or do they just contain a similar prefix?
If they are not exactly the same, please provide some real examples, so we can see exactly what this data we are working with looks like, so we can ensure that we groups them properly.
 
Upvote 0
The original data will always be in columns C and D but will be varied in number of rows, yes it will always be in groups of 6 however the data is varied by numbers and text in each cell.

Essentially what I am looking to do is use cell value rather than specific text but want to put the first 6 cells c1,d1,c2,d2,c3,d3 from c1 to h1 then move to the next 6 cells c4,d4,c5,d5,c6,d6 and put those cells on the next row from c2 to h2 and so forth
 
Upvote 0
Try this (note it will take the data from one sheet and paste it to another, so be sure to update the sheet names at the top of the code to match what you have):
Code:
Sub MyTranspose()

    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim lr As Long
    Dim r As Long
    Dim nr As Long
    Dim i As Long
    
'   Set source worksheet
    Set ws1 = Sheets("Sheet1")
'   Set destination worksheet
    Set ws2 = Sheets("Sheet2")
    
    Application.ScreenUpdating = False
    
'   Find last row on source sheet with an entry in column C
    lr = ws1.Cells(Rows.Count, "C").End(xlUp).Row
    
'   Loop through all rows
    For r = 1 To lr Step 3
'       Calculate row on destination sheet to paste to
        nr = Int(r / 3) + 1
'       Copy over first three rows of data
        For i = 0 To 2
            ws1.Range(Cells(r + i, "C"), Cells(r + i, "D")).Copy ws2.Cells(nr, (i * 2) + 3)
        Next i
    Next r
    
    Application.ScreenUpdating = True
    
    MsgBox "Done!"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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