VBA Copy Array X Times in row

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good evening,

I am looking to copy the array in a row x times. I can get the array to work but how do i expand it if I wanted the array lets say 10 time?
Thanks in advance!

VBA Code:
Sub Copyit()
Dim arr As Variant
arr = Array("Al", "Ant", "Mike", "Don", "Ang", "Tom")
    For i = LBound(arr) To UBound(arr)
        Cells(2, i + 1) = arr(i)
    Next
End Sub

This is the idea!
AlAntMikeDonAngTomAlAntMikeDonAngTomAlAntMikeDonAngTom
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try:
VBA Code:
Sub Copyit()
Dim arr As Variant
arr = Array("Al", "Ant", "Mike", "Don", "Ang", "Tom")
For j = 1 To 10
    For i = LBound(arr) To UBound(arr)
        k = k + 1
        Cells(2, k) = arr(i)
    Next
Next
End Sub
 
Upvote 0
Try:
VBA Code:
Sub Copyit()
Dim arr As Variant
arr = Array("Al", "Ant", "Mike", "Don", "Ang", "Tom")
For j = 1 To 10
    For i = LBound(arr) To UBound(arr)
        k = k + 1
        Cells(2, k) = arr(i)
    Next
Next
End Sub
Thank you so much!!!! That is what I was looking for!!
 
Upvote 0
Here's another way. Note there is no error handling built in if numTimes is set large enough to require more columns than columns.count (~ 16,000 +).
VBA Code:
Sub Copyit()
Const numTimes As Long = 10
Dim arr As Variant, i As Long
arr = Array("Al", "Ant", "Mike", "Don", "Ang", "Tom")
    Application.ScreenUpdating = False
    Do While i < numTimes
        Cells(2, i + 1).Offset(0, i * UBound(arr)).Resize(1, UBound(arr) + 1).Value = arr
        i = i + 1
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Here's another way. Note there is no error handling built in if numTimes is set large enough to require more columns than columns.count (~ 16,000 +).
VBA Code:
Sub Copyit()
Const numTimes As Long = 10
Dim arr As Variant, i As Long
arr = Array("Al", "Ant", "Mike", "Don", "Ang", "Tom")
    Application.ScreenUpdating = False
    Do While i < numTimes
        Cells(2, i + 1).Offset(0, i * UBound(arr)).Resize(1, UBound(arr) + 1).Value = arr
        i = i + 1
    Loop
    Application.ScreenUpdating = True
End Sub
Thanks JoeMo, I appreciate all of the different approaches.
 
Upvote 0
My version:

VBA Code:
Sub Copyit()
'
    Dim LoopTimes   As Long, OffsetValue    As Long, arr    As Variant
    arr = Array("Al", "Ant", "Mike", "Don", "Ang", "Tom")
    OffsetValue = UBound(arr) + 1
    For LoopTimes = 1 To 10
        Range("A2").Offset(0, OffsetValue * LoopTimes - 6).Resize(1, UBound(arr) + 1).Value = arr
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,530
Messages
6,120,071
Members
448,943
Latest member
sharmarick

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