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

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,461
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
How is myArray dimensioned? Can you post all of your code?
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,425
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)?
 

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
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]
 

Zeigdan

New Member
Joined
Sep 9, 2012
Messages
2
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!
 

Forum statistics

Threads
1,137,154
Messages
5,679,914
Members
419,862
Latest member
Bluewings666

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
Top