Print array without looping

gt4894a

New Member
Joined
May 6, 2022
Messages
2
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
This should be stupid simple but I cannot find a solution to save my life. I am trying to print out an array to cells without looping through the array. The calculations for the array take less than a second but placing values onto the page is taking up to 10 seconds. I have run into the same problem in writing python to Excel for another project and found a solution (what I am trying to do now is much more easily done in Excel which is why I am using VBA). I have distilled the problem to the code below. Any help would be much appreciated.

Sub test()

Dim myarr(101) As Double

Range("A1:A1000").Value = "" 'just to clear the last run
For i = 1 To 100
myarr(i) = (i ^ 2 + 0.01) 'arbitrary. just want to fill the array with something.
Next i

Range("A1:A100").Value = myarr 'only gives me a bunch of zeros
'Range("A1:A100").Value = Evaluate(myarr) 'same thing

End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
How about

VBA Code:
Range("A1").Resize(UBound(myarr)) = Application.Transpose(myarr)
 
Upvote 0
Solution
Another option, avoiding transpose
VBA Code:
Sub test()

Dim myarr(1 To 100, 1 To 1) As Double

Range("A1:A1000").Value = "" 'just to clear the last run
For i = 1 To 100
myarr(i, 1) = (i ^ 2 + 0.01) 'arbitrary. just want to fill the array with something.
Next i

Range("A1:A100").Value = myarr 'only gives me a bunch of zeros

End Sub
 
Upvote 0
You are welcome. We were happy to help.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,525
Members
448,969
Latest member
mirek8991

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