3

I am trying to paste Array values into a range but it does only paste in range the first value.

Dim Array1()

For i = 0 to 9
   ReDim Preserve Array1
   Array1(i)= Int((6 * Rnd) + 1)
Next

ws.Range("A1").Value = Array1

output :

enter image description here

Am I doing wrong something ?

3
  • Since you know the array dimension(s) you should declare as a 2D array, with the appropriate dimensions and fill it on the right dimension. ReDim Preserve used often is a kind of memory stress... Commented Nov 26, 2021 at 10:59
  • @FaneDuru Hi, thank you for your feedbacks. I had to use Resize to get this working Commented Nov 26, 2021 at 11:01
  • 1
    Resize must be used, anyhow... I will post an answer only to see what I wonted suggesting. Commented Nov 26, 2021 at 11:04

3 Answers 3

4

You have to redimension the array correctly and then transpose it.

Option Explicit

Sub Sample()
    Dim Array1()
    Dim ws As Worksheet
    Dim i As Long
    
    '~~> Change to respective sheet
    Set ws = Sheet1

    For i = 0 To 9
        ReDim Preserve Array1(i) '<~~ increment by i
        Array1(i) = Int((6 * Rnd) + 1)
    Next
    
    '~~> Store in the worksheet
    ws.Range("A1").Resize(UBound(Array1) + 1, 1).Value = Application.Transpose(Array1)
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

thank you I sawthis but wasn't working since the Transpose is missing in the last row of your code
Yes that is because in that link it is a 2D array. Try the above code. it works.
3

Please, try the next way, in order to fill an already dimensioned 2D array, easy to drop its content in a range:

Sub fill2DArray()
  Dim Array1(1 To 10, 1 To 1), ws As Worksheet, i As Long
  Set ws = ActiveSheet
    For i = 0 To 9
       Array1(i + 1, 1) = Int((6 * Rnd) + 1)
    Next

 ws.Range("A1").Resize(UBound(Array1), UBound(Array1, 2)).value = Array1
End Sub

Transpose has some limitations when used on large ranges.

The next version is your corrected code, but avoiding using ReDim Preserve for each iteration, which stresses the system memory if used too often:

Sub fill1DEfficient()
    Dim Array1(), i As Long, ws As Worksheet, itNo As Long
    
    Set ws = ActiveSheet
    itNo = 10 'number of iterations
    ReDim Array1(itNo - 1)
    For i = 0 To itNo - 1
       Array1(i) = Int((6 * Rnd) + 1)
    Next
   ws.Range("A1").Resize(UBound(Array1) + 1, 1).value = Application.Transpose(Array1)
End Sub

2 Comments

Interesting trick too
And your code could avoid Redim Preserve... Using Redim before the loop. I will edit the answer to show it.
1

One-Column Array to One-Column Range

Option Explicit

Sub WriteArrayToOneColumnRange()
    
    Const rCount As Long = 10 ' Number of Elements (rows in this case)
    Const rndMin As Long = 1
    Const rndMax As Long = 6
    
    ' Define the 2D one-based one-column array.
    Dim Data As Variant: ReDim Data(1 To rCount, 1 To 1)
    
    Dim r As Long ' Element (Row) Counter
    
    ' Populate the array.
    For r = 1 To rCount
        Data(r, 1) = Int(((rndMax - rndMin + 1) * Rnd) + rndMin)
        'Debug.Print Data(r, 1)
    Next r
    
    ' Write the array values to a one-column range starting in 'A1'.
    Dim ws As Worksheet: Set ws = ActiveSheet ' be more specific
    ws.Range("A1").Resize(rCount).Value = Data

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.