initailise array with constant values

jimrward

Well-known Member
Joined
Feb 24, 2003
Messages
1,877
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
looking for a concise VBA method to fill an array with a constant value without using a loop

in fortran you can use data statements or inline declaration for example
Code:
CHARACTER*3 MYARRAY(10)/10 * "abc"/
 
If a 0 based array is acceptable
Code:
Dim myString As String
Dim myArray As Variant
Dim mySize As Long

mySize = 10
myString = "initial Value"

myArray = Split(Mid(Application.Rept(Chr(5) & myString, mySize), 2), Chr(5))
A few comments.

1. This is quite similar to my suggestion in post #5 but I do like the idea of putting the delimiter before the given string and then using Mid.

2. I still don't see a way to make this work to fill the array with numerical values.

3. I don't know how big an array the OP might be wanting to fill, but using Application.Rept is more restrictive in that regard than the slightly longer construct I used to produce the string to split. For example, with your existing code, try changing mySize to 3000

So, adopting part of Mike's idea with part of mine from post #5, another way to fill a zero-based 1-dimensional array of string values:
Rich (BB code):
Sub FillArray1a()
  Dim myArray As Variant

  Const Size As Long = 10000
  Const Value As String = "initial value"

  myArray = Split(Mid(Replace(Space(Size), " ", Chr(0) & Value), 2), Chr(0))
'  ReDim Preserve myArray(1 To Size) '<- To convert to a 1-based array
End Sub
Again, if a 1-based array is required, it can be converted by using the commented line
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
But why didn't you use the first construction for numbers also? The following seems to work for me (am I missing something?)
Rich (BB code):
Sub FillArray4()
   Dim MyArray As Variant
  
  Const Size As Long = 10
  Const Value As Single = 5.2
  
  MyArray = Evaluate("IFERROR(TRANSPOSE(ROW(1:" & Size & ")/0),""" & Value & """)")
  MsgBox TypeName(MyArray(1))
End Sub
Rick, that fills each element of the array with the String "5.2" rather than the Number 5.2 per FillArray5


Edit: But removing the extra quote marks does appear to work - though I was sure I tried this yesterday and couldn't get it to work. :(
Rich (BB code):
Sub FillArray4a()
  Dim MyArray As Variant
  
  Const Size As Long = 10
  Const Value As Single = 5.2
  
  MyArray = Evaluate("IFERROR(TRANSPOSE(ROW(1:" & Size & ")/0)," & Value & ")")
End Sub
 
Last edited:
Upvote 0
Upvote 0
thank you all for some interesting takes, I will try a few to see how I get on, I have in the meantime put together a version of what I need using temp work areas and cells, as execution speed is not an issue
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,454
Members
449,083
Latest member
Ava19

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