Results 1 to 5 of 5

Thread: What in the....

  1. #1

    Thread Starter
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    What in the....

    This is Very werid, it's as if VBA in Excel 2003 has a bug....

    If I do:
    VB Code:
    1. For i = 0 To ListBox1.ListCount - 1            
    2.             If ListBox1.Selected(i) Then
    3.                 ListBox2.AddItem ListBox1.List(i)
    4.             End If
    5. Next
    I get an error stating that it cannot get the selected property. When debugging I see that i is the same as Listbox1.ListCount, therefore, it is trying to get the selected value of an element that does not exist. So, I am having to put it as follows:
    VB Code:
    1. For i = 0 To (ListBox1.ListCount - 1)
    2.         If i = ListBox1.ListCount Then Exit For
    3.             If ListBox1.Selected(i) Then
    4.                 ListBox2.AddItem ListBox1.List(i)
    5.             End If
    6. Next
    Any ideas or thoughts about this?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: What in the....

    try to address the object that contains the listbox

    for i = 0 to sheet4.listbox.listcount -1

    should all work
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: What in the....

    Yeah, I tried all that. For whatever reason the For Next loop is simply going over the Listbox.Count.... Even in some cases by more than one... I found this out thanks to some error handling.

    It's the oddest thing, I have never had this problem before.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: What in the....

    that is a common problem if you are deleting items from the listbox in the for loop, in which case you should use

    VB Code:
    1. for i =  sheet4.listbox.listcount -1 to 0 Step - 1
    to loop from the bottom
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: What in the....

    I am not deleting items from the listbox, I am loading each item into a variable. I have it handled, but I just hate having to handle something that should be assumed. You know?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width