VBA Copy Array X Times in row

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,125
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

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
4,136
Office Version
  1. 365
Platform
  1. Windows
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

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,125
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
18,069
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,125
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,265
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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,191,381
Messages
5,986,300
Members
440,017
Latest member
vasanrajeswaran

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
Top