VBA to transpose rows to columns with varying rows (and remove first character)

CraigG

Board Regular
Joined
May 1, 2005
Messages
169
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
  2. Mobile
Hi

I have an Excel file (exported from a system) which has rows of data. I need the rows transposing to columns. The problem I have is that the set of rows varies so it's not as simple as rows 1-5, then 6-10, 11-15, etc to transposing columns. An example of my data is below:

COLUMN A:
^
D5/25/15
U600.00
T600.00
CX
PTEST1
MMEMO1
LCATEGORYA
^
D3/11/16
U-600.00
T-600.00
NTXFR
PTEST1
MMEMO1
LCATEGORYB
^

<tbody>
</tbody>
D10/3/17
U-169.20
T-169.20
NTXFR
PTEST3
LCATEGORYAA
SCATEGORYAA
EMEMOA1
$-56.40
SCATEGORYAB
EMEMOA2
$-56.40
SCATEGORYAC
EMEMOA3
$-56.40
^

<tbody>
</tbody>

I want the above transposing to columns by matching the first character of the row and putting it in a column, eg, all rows starting with D go in column A (and remove the first char) - eg, row 2, 'D5/25/15' into column A '5/25/15'. The '^' symbols denotes the start and end of the 'set' of data in the rows. I have 1000s of rows so can't do it manually. The above would look like...


A--------------B------------C--------------D-----------E------------F----------------G----------------H------------I
(prefixed with... in the rows...)
D-------------U-------------T--------------$------------C-----------N----------------P----------------M------------L and S



5/25/15------600.00------600.00----------------------X----------------------------TEST1-----------MEMO1------CATEGORYA


3/11/16------(600.00)----(600.00)---------------------------------TXF-------------TEST1----------MEMO1------CATEGORYB


10/3/17------(169.20)----(169.20)-------------------------------- TXF-------------TEST3------------------------CATEGORYAA
10/3/17-----------------------------------(56.40)-----------------------------------------------------MEMOA1----CATEGORYAA
10/3/17-----------------------------------(56.40)-----------------------------------------------------MEMOA2----CATEGORYAB
10/3/17-----------------------------------(56.40)-----------------------------------------------------MEMOA3----CATEGORYAC



You will see the added complication is the last 'set' of rows where the format is slightly different and requires the date (prefixed with D) copied again for each row.

Can anyone help with any VBA?

Thanks in advance.

Craig
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try this, it should get you started. Be sure to change the name of the sheet in the code
Code:
Sub  transpose_rows_columns()    
Dim arr() As Variant
    
    'get last column in first row
    lcol = Cells(1, Columns.Count).End(xlToLeft).Column
    'make array the length of cols
    ReDim arr(lcol)
    
    'get largest row in current column
    For i = 0 To lcol - 1
        lrow = Cells(Rows.Count, i + 1).End(xlUp).Row
        If largestRow < lrow Then
            largestRow = lrow
        End If
    Next
    'make array the size of the table
    ReDim arr(lcol - 1, largestRow - 1)
    'loop through columns
    For i = 1 To lcol
        'get last row in current column
        lrow = Cells(Rows.Count, i).End(xlUp).Row
        'loop through rows
        For j = 1 To lrow
            'assign each row in column to array and remove first value
            arr(i - 1, j - 1) = Mid(Cells(j, i).Value, 2, Len(Cells(j, i).Value))
        Next
    Next
    'clear contents of the sheet
    Sheets("Name_of_sheet").UsedRange.ClearContents
    'transpose values in array
    For i = LBound(arr) To UBound(arr)
        For j = 0 To UBound(arr, 2)
            Cells(i + 1, j + 1) = arr(i, j)
        Next
    Next
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,137
Messages
6,123,254
Members
449,093
Latest member
Vincent Khandagale

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