Filling a VBA array with a list of values ​​in native code

Hoping to quickly answer this question ....

I have an array and I want to populate it with a list of arguments.

Sub EasyArrayInput() Dim myArr() as variant myArr = ("string1", "string2", "string3") End Sub 

I'm good at knowing how to iterate and populate with for / next or do / while, but it would be nice to be able to populate an array when the values ​​don't change without using a hard-coded method.

 Sub UsualMethodThatIDontWantToDo() Dim myArr(1 to 3) as variant myArr(1) = "string1" myArr(2) = "string2" myArr(3) = "string3" End Sub 

Is there a way to do this in a method similar to the first piece of code? I would rather do it that way. I apologize if this question is asked / answered, but I'm not quite sure that I called the method I'm asking for.

Thanks in advance!

Edit: solution

Below is a snippet of code (from a link sent by chance) that will create an array, which is an option and an extension of what I wanted.

 Sub EasyArrayInput() Dim myArr() as variant myArr = Array("string1", "string2", "string3") End Sub 

The following code snippet looks useful if you only have lines and you do not want to initialize the option:

 Sub EasyArrayInput() Dim myArr() as String myArr = Split("String1,String2,String3", ",") End Sub 
+6
source share
2 answers

What about?

 Sub EasyArrayInput() Dim myArr() As Variant myArr = Array("string1", "string2", "string3") End Sub 
+3
source

Assuming you have some numerical sequence, you can do something like this:

 Dim myArray() myArray = [TRANSPOSE(INDEX("string"&ROW(1:10),))] 

but honestly, I think the cycle is clearer.

+2
source

Source: https://habr.com/ru/post/982749/


All Articles