Writing Array to Range

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Afternoon

I've been attempting to understand arrays and in particular how to write an array (see below) to a range.

This code does not include the last element "July" - not sure why?

Thanks for any help/explanation



Code:
Dim arr As Variantarr = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "July")


[D2].Resize(UBound(arr)) = Application.Transpose(arr)


If I utilize this code, which places all values in the array then copies to range F1. I have no problem.

Code:
Dim myarray As Variant



myarray = Range("C1", Range("C" & Rows.Count).End(xlUp)).Value


[F1].Resize(UBound(myarray)) = myarray
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I suspect yor problem is because by default excel will define an array starting with index zero, so your array has elements 0 to 6
while if you load an array from a worksheet is will by default be starting at 1 , this is because there isn 't a row zero on trhe worksheet
 
Last edited:
Upvote 0
offthelip is correct about the reason why you are "missing" the last element of your array. However, since your array elements are fixed text constants, there is another way you can populate the arr variable so you don't need the Transpose function call. Note that there is a slight variation for an array that will be assigned down a column of cells (semi-colon delimiter) as opposed to across a row of cells (comma delimiter).
Code:
' Semi-colon delimiter
Sub PutArrayElementsDownTheColumn()
  Dim arr As Variant
  arr = [{"Jan";"Feb";"Mar";"Apr";"May";"Jun";"July"}]
  [D2].Resize(UBound(arr)) = arr
End Sub

' Comma delimiter
Sub PutArrayElementsAcrossTheRow()
  Dim arr As Variant
  arr = [{"Jan","Feb","Mar","Apr","May","Jun","July"}]
  [F1].Resize(, UBound(arr)) = arr
End Sub
 
Upvote 0
Thank you Mr. Rothstein
question if I added a Blank " " as a work around. Then the blank would be the last element
 
Upvote 0
Thank you Mr. Rothstein
question if I added a Blank " " as a work around. Then the blank would be the last element
If you are talking about using the Array function (as you originally posted), wouldn't it be easier (or at least as easy) to simply add one to the UBound function's return value as offthelip posted? If, on the other hand, you are talking about the alternatives I posted to using the Array function, there is no need to adjust anything as the arrays are two dimensional arrays starting with index number 1 for both dimensions.
 
Upvote 0

Forum statistics

Threads
1,215,067
Messages
6,122,949
Members
449,095
Latest member
nmaske

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