By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,808 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,808 IT Pros & Developers. It's quick & easy.

List.Listcount property not working

P: 2
My code:

If List.Listcount > 0 then
For intI = 0 to List.Listcount - 1
'code
Next intI
End If

For some reason, and not all the time, this loop only runs to the second last entry. I have even witnessed it in break mode and following it step by step with Shift + F8, where it reports - for example - that the listcount is 14, when if fact it is 15 and has index values of 0 to 14. I made a quick fix, but I don't like this solution as it affects my ability to bug test the rest of the sub.

Quick and dirty fix:

On Error Resume Next
If List.Listcount > 0 then
For intI = 0 to List.Listcount
'code
Next intI
End If

This way if listcount is correct, the loop will error on the last value, but will simply resume after the loop, but when it reports it incorrectly, it should still retrieve all items in the list.

My question is why is the listcount property not reporting the correct value, if anybody else has encountered this problem, and if anybody knows a better fix for this. (ps. the ubound property is even less reliable for me)
Apr 12 '07 #1
Share this Question
Share on Google+
5 Replies


iburyak
Expert 100+
P: 1,017
It usually happens when you delete items from list.
In this case you have to go opposite way

Expand|Select|Wrap|Line Numbers
  1. If List.Listcount > 0 then
  2. For intI = List.Listcount  1 to 0 Step -1
  3. 'code
  4. Next intI
  5. End If

Good Luck.
Apr 13 '07 #2

P: 58
My code:

If List.Listcount > 0 then
For intI = 0 to List.Listcount - 1
'code
Next intI
End If

For some reason, and not all the time, this loop only runs to the second last entry. I have even witnessed it in break mode and following it step by step with Shift + F8, where it reports - for example - that the listcount is 14, when if fact it is 15 and has index values of 0 to 14. I made a quick fix, but I don't like this solution as it affects my ability to bug test the rest of the sub.

Quick and dirty fix:

On Error Resume Next
If List.Listcount > 0 then
For intI = 0 to List.Listcount
'code
Next intI
End If

This way if listcount is correct, the loop will error on the last value, but will simply resume after the loop, but when it reports it incorrectly, it should still retrieve all items in the list.

My question is why is the listcount property not reporting the correct value, if anybody else has encountered this problem, and if anybody knows a better fix for this. (ps. the ubound property is even less reliable for me)

Hi...

I try to evaluate that in my PC... but i dont see any problem...
Heres my code:

If LstGroups.ListCount > 0 Then
For i = 0 To LstGroups.ListCount - 1
Debug.Print i
Next i
End If


On my Immediate window i successfully see the output from 0-14 of 15 records... and it has no error...

What was the error message?


GoodLuck...
Apr 13 '07 #3

P: 2
It does not give me any error. This loop is pulling the list entries from a list that is on another form. Each time I return to my parent form and add something new to the list, it shows what I added to the list just before that item in the view form (after it again executes this loop).
Apr 14 '07 #4

Expert 5K+
P: 8,434
It does not give me any error. This loop is pulling the list entries from a list that is on another form. Each time I return to my parent form and add something new to the list, it shows what I added to the list just before that item in the view form (after it again executes this loop).
If no error occurs, then your addition of On Error code will have no effect.

The only way I can think of that this could happen is if the number of items in the list changes during the execution of the loop. Don't forget, the limits of the loop are set at the start, so if the number of items in this list decreases during execution of the loop, you will simply go past the end of the list and start returning empty strings. (Try it - List(n) just returns nothing, if n is too high.)
Apr 14 '07 #5

Expert 5K+
P: 8,434
Sorry, I just re-read your original post. I see you have increased the loop end and put in the On Error to catch it. But as I said, going past the end of the List() array doesn't produce an error - it simply returns no string. So the On Error will still achieve nothing.
Apr 14 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.