VBA to transpose rows to columns by 'n' times

anglais428

Well-known Member
Joined
Nov 23, 2009
Messages
634
Office Version
  1. 2016
Platform
  1. Windows
Current data view:

A
B
C
D
E

<tbody>
</tbody>

Required data view:
ABCDEABCDE

<tbody>
</tbody>

This is taking 5 cells listed in rows and converting to 10 cells listed in columns (1 row).
Ideally the multiple of 2 (5 * 2 = 10) would be dynamic.

Thanks
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
This macro assumes the data you want to repeat is in column A starting in row 1. You will be prompted for the number of times you want the data repeated.
Code:
Sub TransposeData()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim lColumn As Long
    Dim response As Long
    response = InputBox("How many times do you want the data repeated?")
    Dim x As Long
    For x = 1 To response
        lColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column + 1
        Range("A1:A" & LastRow).Copy
        Cells(1, lColumn).PasteSpecial Transpose:=True
    Next x
    Columns(1).EntireColumn.Delete
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Here is another macro for you to consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub TransposeAndRepeat()
  Dim LastRow As Long, Response As Long, Arr As Variant
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Response = Application.InputBox("How many times do you want to you want the data repeated?", Type:=1)
  Arr = Split(Application.Rept(Join(Application.Transpose(Range("A1:A" & LastRow)), Chr(1)) & Chr(1), Response), Chr(1))
  Range("A1").Resize(, UBound(Arr) + 1) = Arr
  Range("A2:A" & LastRow).ClearContents
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Hi, to all!

Another option could be:
Code:
Sub TransposeWithRep()    
    Dim n&, q&
    
    n = Application.InputBox(" Times to repeat? ", , 2): If n <= 0 Then Exit Sub
    With Range("A1").CurrentRegion.Columns(1)
        q = .Rows.Count: .Copy
    End With
    
    With Range("C1")
        .PasteSpecial xlPasteValues, , , True
        .Resize(, q).AutoFill .Resize(, q * n)
        .Select: Application.CutCopyMode = False
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,453
Members
448,898
Latest member
drewmorgan128

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