VBA transpose data rows to 2 columns

bunburyst

New Member
Joined
Apr 18, 2018
Messages
24
Hi, please, I need a vba code to transpose my data
from the following in sheet 1:

Data range is B3:IZ734

1.jpg


to the following in sheet 2:

2.jpg


A working code would be greatly appreciated,

Tks.

Remy
 
@mohadin , thanks so much, macro work perfectly!, Just one more thing, when transposing the dates the format is not respected. For example, January 2, 2016 becomes 01/02/2016, american format. I need to respect the dates, european format. 02/01/2016
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
VBA Code:
Sub test()
    Dim a, b
    Dim i, x
    Dim lr, lc
    With Sheets("sheet1")
        lr = .Cells(Rows.Count, 3).End(xlUp).Row
        lc = .Cells(3, Columns.Count).End(xlToLeft).Column
        ReDim a(1 To (lr - 2) / 2)
        For i = 1 To UBound(a)
            a(i) = .Cells(1 + i * 2, 2).Resize(2, lc)
        Next
        With Sheets("sheet2")
            For i = 1 To UBound(a)
                .Cells(3 + x, 2).Resize(UBound(a(i), 2), 2) = Application.Transpose(a(i))
                x = UBound(a(i), 2) + x - 1
            Next
            Columns("B:B").NumberFormat = "d/m/yyyy"
        End With
    End With
End Sub
 
Last edited:
Upvote 0
Thanks my friend but It does not respect the date, the macro continues to transform the dates, for example, January 2 is transposed to February 1.
 
Upvote 0
Option set up as Array formula

VBA Code:
Sub transpose_data_rows_to_2_columns()
Dim SourceRng As Range
Dim TrnspsRng As Range

    Set SourceRng = Worksheets("Sheet1").Range("B3:IZ734")
    Set TrnspsRng = Worksheets("Sheet2").Range(SourceRng.Cells(1, 1).Address).Resize(SourceRng.Columns.Count * (SourceRng.Rows.Count / 2), SourceRng.Rows.Count)
  
    ArrSrcRng = SourceRng.Parent.Name & "!" & SourceRng.Address
    RwNum = "ROW()-ROW(" & SourceRng.Cells(1, 1).Address & ")+1"
    
    TrnspsRng.Columns(1).Cells.FormulaArray = "=INDEX(" & ArrSrcRng & ",(CEILING(" & RwNum & ",COLUMNS(" & ArrSrcRng & "))/COLUMNS(" & ArrSrcRng & ")*2)-1,MOD(" & RwNum & "-1,COLUMNS(" & ArrSrcRng & "))+1)"
    TrnspsRng.Columns(2).Cells.FormulaArray = "=INDEX(" & ArrSrcRng & ",((CEILING(" & RwNum & ",COLUMNS(" & ArrSrcRng & "))/COLUMNS(" & ArrSrcRng & ")*2)-1)+1,MOD(" & RwNum & "-1,COLUMNS(" & ArrSrcRng & "))+1)"
  
End Sub
 
Upvote 0
Solution
maybe Power Query instead of vba
Column1Column2Column3Column4Column5Column6Column7Column8Column9Column10Column11Column1Column2
01/01/201601/01/201601/01/201601/01/201601/01/201601/01/201601/01/201601/01/201601/01/201601/01/201601/01/201601/01/201656
561753385154363042511701/01/201617
02/01/201602/01/201602/01/201602/01/201602/01/201602/01/201602/01/201602/01/201602/01/201602/01/201602/01/201601/01/201653
62141644255362848314901/01/201638
01/01/201651
01/01/201654
01/01/201636
01/01/201630
01/01/201642
01/01/201651
01/01/201617
02/01/201662
02/01/201614
02/01/201616
02/01/201644
02/01/201625
02/01/201653
02/01/201662
02/01/20168
02/01/201648
02/01/201631
02/01/201649

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FN = Table.FirstN(Source,2),
    TFN = Table.Transpose(FN),
    LN = Table.LastN(Source,2),
    TLN = Table.Transpose(LN),
    TC = Table.Combine({TFN, TLN}),
    Type = Table.TransformColumnTypes(TC,{{"Column1", type date}})
in
    Type
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,142
Members
448,551
Latest member
Sienna de Souza

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