Problems transposing a table (columns into rows, with column headings)

LondonExcel

New Member
Joined
Aug 10, 2011
Messages
12
Having read what seems like hundreds of forum posts about using a macro to transpose data from columns into rows, I still haven't managed to get my macro to work.

I need to go from this:
1234
Category ARecord 1BCDE
Category ARecord 2CDE
Category BRecord 3CA
Category BRecord 4A

<tbody>
</tbody>

To this:
Category A
Record 1
1B
2C
3D
4E
Record 2
1C
2D
3E
Category B
Record 3
1C
4A
Record 4
3A

<tbody>
</tbody>



Here's what I have so far:

Code:
Sub TransposeColumnsToRows()


Dim sourceCell As Range
Dim destCell As Range
Dim lastRowInDestCol As Range
Dim category As String


category = Sheets("Source data").Range("A2")
Sheets("Source data").Range("L1") = category

Do While Sheets("Source data").Range("A2").Value <> ""

If category <> Sheets("Source data").Range("A2") Then

'Select first row and header row
    Sheets("Source data").Range("A1:J2").Select
'Copy first row and header row
    Selection.Copy
'Select pasting location
    Sheets("Source data").Range("L2").Select

Else

'Select all but the category column
    Sheets("Source data").Range("B1:J2").Select
'Copy first row and header row
    Selection.Copy

End If

lastRowInDestCol = Range("J" & Rows.Count).End(xlUp).Row + 1

'Select first empty row in destination column
    Range("J" & lastRowInDestCol).Select

'Paste with transpose
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=True

'Select and delete row that was just transposed
    Sheets("Source data").Range("A2:J2").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp

Loop

End Sub


Any thoughts/suggestions on how best to achieve this?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,216,144
Messages
6,129,120
Members
449,488
Latest member
qh017

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