Rearranging Data from Columns to Rows

CJ0206

New Member
Joined
Aug 1, 2018
Messages
1
Hi All,

I am currently looking to use a VBA code for transposing data.

I currently have a sheet set up which has 19 columns of data that i wish to keep in place and then and addition 7 columns which repeat across the sheet. It is these additional columns that i wish to transpose onto a new row while copying the original 19 columns of data.

Originally I had 19 fixed columns and 3 that needed to be transposed, the number of columns to be transposed has now increased to 7, while I think I have figured out the code to allow for this to happen it appears that additional columns are only reflecting the values found in the first set of 7 columns.

Could you please look over the below and advise if I have missed something when changing the VBA. Please see below:

Code:
Sub Rearrange_All()
Dim a As Variant, b As Variant
Dim i As Long, j As Long, k As Long, ubA2 As Long, c As Long
 
a = Range("A1").CurrentRegion.Value
ubA2 = UBound(a, 2)
ReDim b(1 To UBound(a) * (ubA2 - 19) / 7, 1 To 26)
For i = 2 To UBound(a)
For j = 20 To ubA2 Step 7
If Len(a(i, j)) Then
k = k + 1
For c = 1 To 26
b(k, c) = a(i, c)
Next c
b(k, 20) = a(i, j): b(k, 21) = a(i, j + 1): b(k, 22) = a(i, j + 2)
End If
Next j
Next i
With Range("A" & Rows.Count).End(xlUp).Offset(7).Resize(, 26)
 
.Offset(1).Resize(k).Value = b
End With
End Sub

If you could please advise it would be a greatly appreciated.
 
Last edited by a moderator:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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