Array in condition of Do While loop in VBA

Zeigdan

New Member
Joined
Sep 9, 2012
Messages
2
Please help me understand where my syntax is wrong here! I'm a beginner in vba but am learning fast. I'll try to simplify the problem below.

I have created an array called myArray(). It has 5 values, 1,2,3,4, and5. I've confirmed the array was created successfuly. I then have a loop where h is equal to 5 to start:

Do While h > 0
dExpMean = Exp(-dMean)
dFact = 1
dPowr = 1
Do While dCDF < myArray(h)
dCDF = dCDF + dExpMean * dPowr / dFact
iX = iX + 1
dFact = dFact * iX
dPowr = dPowr * dMean
Loop
ReDim Preserve PoisArray(h)
PoisArray(h) = iX - IIf(dCDF / myArray(h) > 1.000000000001, 2, 1)
h = h - 1
Loop

when stepping through the code, it makes it to the highlighted line in red, and then begins to jump back up to higher code lines not shown here. I've input the number 5 instead of "myArray(h)", and the code works fine and steps into the nested loop. It appears that it doesn't like using the array reference as a condition in the nested loop. Is this a limitation? Any ideas on how to make it work? Any help is most appreciated.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
How is myArray dimensioned? Can you post all of your code?
 
Upvote 0
It's not a limitation.

When you step through, what is the value of h and what is the index range of myArray (0 to 4 or is it 1 to 5)?
 
Upvote 0
Zeigdan,

Welcome to the MrExcel forum.

Can we see your complete macro code?


If posting VBA code, please use Code Tags - like this:

[code]

'Paste your code here.

[/code]
 
Upvote 0
Oh you guys are right on! It was trying to reference the 5th place holder in the array but my values 1-5 were stored in place holder 0,1,2,3,4. This fixed it, Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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