While Loop with Type Mismatch

shadyferret

New Member
Joined
Mar 22, 2012
Messages
7
I have a while loop that counts backwards in a seriescollection to find the last value with data. In one particular case, I had to use an IF formula in the source data where it would output NA() if the statment was false, so instead of loop through empty strings until the loop finds a number, it loops through #NA until it finds a number.

This ALMOST works. In Debug mode, the loop actually returns a value of 141, which is what I want, but it won't store it in a variable. Help?

Code:
Dim arrRedLimit As Variant
Dim dblRedLimit As Double
Dim i As Long
Dim varErr As Variant

varErr = CVErr(xlErrNA)

arrRedLimit = ActiveChart.SeriesCollection("Upper Bound").Values
While IsEmpty(arrRedLimit(i))
i = i - 1
Wend
dblRedLimit = arrRedLimit(i)

Can anyone see the error?


(P.S. If you're wondering why I would have "NA" as an output, its because it won't graph, where putting "" for my false condition did)
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I was thinking that maybe th i = i- 1 might cause the value of i to change for the statement dblRedLimit = arrRedLimit(i), but I am not sure. That is just a first glance. If it does, then that would make arrRedLimit(i) = something other than the first found value, or Nothing, depending on what the series collection held. I haven't used While in a long time, so not sure when it would acturally kick out of the loop. I will have to check that.
 
Upvote 0
That was not the answer. It pops right out of the loop as soon as it hits a value. So, the only other thing I can think of is to add the property .Value like:

dblRedLimit = arrRedLimit(i).Value
 
Upvote 0
I'll try that. The code works fine for all of my other series, but not the one with "NA()" in it, so I'm not sure if that will fix it.

Thanks for your help!
 
Upvote 0
In this line

While IsEmpty(arrRedLimit(i))

you are checking empty values but not the #N/A error.

Try something like

While IsEmpty(arrRedLimit(i)) Or arrRedLimit(i)=VarErr
 
Upvote 0
Oh oops, I should have better checked what I was coping. I did that and its how I got the right number (the 141) but it won't store the value as a variable.
 
Upvote 0
What do you mean by
...but it won't store the value as a variable.
and what about the statement
Code:
[FONT="Consolas"][SIZE="2"][COLOR="Navy"]dblRedLimit = arrRedLimit(i)[/COLOR][/SIZE][/FONT]

Does this line generate error or what?
 
Upvote 0
What do you mean by

and what about the statement
Code:
[FONT=Consolas][SIZE=2][COLOR=navy]dblRedLimit = arrRedLimit(i)[/COLOR][/SIZE][/FONT]

Does this line generate error or what?


Yes, this line has an error, although I'm not sure why. In Debug mode, hovering over arrRedLimit(i) in that line shows the number 141, but hovering over dblRedLimit here and the next its used shows that dblRedLimit is empty. The program terminates because of a type mismatch. I've delcared dblRedLimit as Long, so I'm not sure what's going on.
 
Upvote 0
Have you tried using IsError?
Rich (BB code):
For I = LBound(arrRedLimit) To UBound(arrRedLimit)
    If IsError(arrRedLimit) Then Exit For
Next I
The For will be exited when the first error value, ie NA, is found.

The loop variable, I, will actually point to the error but If we assume the value before that isn't an error we can return it with I-1.

So after the loop.
Rich (BB code):
dblRedLimit = arrRedLimit(I-1)
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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