Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
vvvvvvizard
Partner - Specialist
Partner - Specialist

vba to populate an array

am trying to populate an array , i can do it manually like this

sitearray=Array(varsplit(1),varsplit(2) ,varsplit(3),varsplit(4) ...........)

and it works

How do i populate the array using a loop statement

i tried

for z=1 to 10

     sitearray=Array(varsplit(z))

Next z

and i get an expected end of statement error

Please assist me with the correct syntax

1 Solution

Accepted Solutions
marcus_sommer

Try:

dim sitearray(), varsplit, vara, arrCount, i

vara= ActiveDocument.Variables("vAlias").getcontent.string

varsplit=Split(vara,",")

arrCount = ubound(varsplit)

redim sitearray(arrCount)

for i = 0 to arrCount - 1

     sitearray(i) = varsplit(i)

next ' without i

But why sitearray if varsplit contained the same?

- Marcus

View solution in original post

10 Replies
marcus_sommer

Try something like this:

for z=1 to 10

     sitearray(z) = varsplit(z)

Next z

Understanding VBScript: Arrays | Scripting content from Windows IT Pro

- Marcus

vvvvvvizard
Partner - Specialist
Partner - Specialist
Author

even if i try a

dim sitearray

  sitearray(0)=Array('aaaa') or a sitearray(1)=Array('aaaa')   i get a type mismatch error

vba sucks

marcus_sommer

You need to declare sitearray as ARRAY and sometimes to dimension it:

dim sitearray(), z 'another ...

redim sitearray(9) 'started with 0

- Marcus

vvvvvvizard
Partner - Specialist
Partner - Specialist
Author

Hi Marcus , i marked your  response as helpful as the url gave me insight into static vs dynamic arrays , i can now populate arrays but not with a loop , for now il  copy and paste 324 time and fill in the numbers to populate the array .

Im certain theres an easier way to do it with a loop , i think my for statement might be the issue now .

Dim sitearray(324)

'Dim z

  sitearray(0)=varsplit(1)

  sitearray(1)=varsplit(2)

  sitearray(2)=varsplit(3)

........................

until sitearray(323)=varsplit(323)

marcus_sommer

How will be varsplit filled - is this an array, too? Maybe you posted a little more from your code.

- Marcus

vvvvvvizard
Partner - Specialist
Partner - Specialist
Author

Option Explicit

Dim vara

vara= ActiveDocument.Variables("vAlias").getcontent.string

'Split the variable int its parts

Dim varsplit

varsplit=Split(vara,",")

'Dim sitearray(3)

'Dim z

  'sitearray(0)=varsplit(1)

  'sitearray(1)=varsplit(2)

  'sitearray(2)=varsplit(3)

  'sitearray(3)=varsplit(4)

  'sitearray(3)=varsplit(4)

  'sitearray(3)=varsplit(4)

  'sitearray(3)=varsplit(4)

'Msgbox(sitearray(0))

Sub GenerateBarChart()

i can populate the array this way but not with a for loop statement as stated

vvvvvvizard
Partner - Specialist
Partner - Specialist
Author

first time am trying a loop statement in vba

even if i try

For i=1 to 10

msgbox("aaaaaa")

Next i

i get an expected end of statement error on the next i line

vvvvvvizard
Partner - Specialist
Partner - Specialist
Author

I figured it out  , its

For z=1 to 10

  statements

next

its just next , not next Z which gives an error , seems that vba used in those excel sites is different from the vba used in  Qlikview ,not sure why .

marcus_sommer

Try:

dim sitearray(), varsplit, vara, arrCount, i

vara= ActiveDocument.Variables("vAlias").getcontent.string

varsplit=Split(vara,",")

arrCount = ubound(varsplit)

redim sitearray(arrCount)

for i = 0 to arrCount - 1

     sitearray(i) = varsplit(i)

next ' without i

But why sitearray if varsplit contained the same?

- Marcus