adding array elements by index

eviMogwai

New Member
Joined
Jul 4, 2011
Messages
26
Thanks for stopping by =)

So, I have a reference array I want to build a subset of for use in a function.
The reference is a long array of values, the array I need will only need some of the values and as I know the positions in the reference, i thought I'd set up a for-loop.

Code:
dim vArr(), vRef
vRef = Array(10,20,30,40,50,60,70,80)
 
for i = 0 to 5
 
debug.print(vRef(i))
 
vArr(i) = vRef(i)
next

Unfortunately, this gives me an index out of bound for the first step in the for-loop.
The direct window gives an output of 10, so vRef(0) seems to work as it should.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
vArr has no dimension. So you cant reference vArr(i)

Try something like this...

Code:
Dim vArr(), vRef
vRef = Array(10, 20, 30, 40, 50, 60, 70, 80)
[COLOR="Red"]ReDim vArr(0 To 5)[/COLOR]
For i = 0 To 5
 
Debug.Print (vRef(i))
 
vArr(i) = vRef(i)
Next
 
Upvote 0
Hmm, in my function I need vArr to change it's length according to a range elsewhere in the program.
From what I've read this means I need to redim it in the loop something like:
Code:
Dim vArr(), vRef
vRef = Array(10, 20, 30, 40, 50, 60, 70, 80)
 
For i = 0 To 5
 
[COLOR=red]ReDim Preserve vArr(0 To Ubound(vArr) +1)[/COLOR]
vArr(i) = vRef(i)
Next

But this also gives an Index out of Bounds -error
 
Upvote 0
You are trying to use UBound before the array has been dimensioned.

Use a counter, I might be a good candidate, to redimension rge array.
 
Upvote 0
You can ReDim Preserve only if vArr already has a dimension.

Try this...
Code:
Dim vArr(), vRef
[COLOR="Red"]ReDim vArr(0)[/COLOR]
vRef = Array(10, 20, 30, 40, 50, 60, 70, 80)
 
For i = 0 To 5
vArr(i) = vRef(i)
ReDim Preserve vArr(0 To UBound(vArr) + [COLOR="Red"]1[/COLOR])
Next

Or this if you know how many times you will loop.
Code:
Dim vArr(), vRef
ReDim vArr(0)
vRef = Array(10, 20, 30, 40, 50, 60, 70, 80)
ReDim Preserve vArr(0 To UBound(vArr) + [COLOR="Red"]6[/COLOR])
For i = 0 To 5
vArr(i) = vRef(i)
Next
 
Last edited:
Upvote 0
Ah, nice catch!
I now rebuilt it to look like
Rich (BB code):
Dim vArr(), vRef
vRef = Array(10, 20, 30, 40, 50, 60, 70, 80)
 
For i = 0 To 5
 
ReDim Preserve vArr(0 To i)
vArr(i) = vRef(i)
Next

Which gives me possibility to change how long the for-loop goes without rewriting the function.

Thank you!
 
Upvote 0
No problem.

Any chance you could post the function all this is for?

Or at least an idea of it's purpose.:)
 
Upvote 0
It's an implementation of EN ISO 717-2:1996, which uses a very analog way of calculating a reference value =)

The code is complex and hairy and written in swedish. But in principle it takes noise levels for a set of bandwidths, compares them to a curve, manipulate the curve according to standardized rules to get a value that represents the step noise insulation of the noise levels.

This particular part would be used to calculate a correction term which allows different bandwidth ranges than the insulation standard.
The code above would feed the non-standard bandwidth levels to the standard calculator, ignoring the bandwidths not included in the standard =)

Hope it wasn't too technical, always hard when referencing to a standard
 
Upvote 0
Sounds interesting, but it also sounds like something I wouldn't have a clue about.:)
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,948
Latest member
UsmanAli786

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