|
-
Sep 22nd, 2006, 11:22 AM
#1
Thread Starter
Frenzied Member
What in the....
This is Very werid, it's as if VBA in Excel 2003 has a bug....
If I do:
VB Code:
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
ListBox2.AddItem ListBox1.List(i)
End If
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:
For i = 0 To (ListBox1.ListCount - 1)
If i = ListBox1.ListCount Then Exit For
If ListBox1.Selected(i) Then
ListBox2.AddItem ListBox1.List(i)
End If
Next
Any ideas or thoughts about this?
-
Sep 22nd, 2006, 06:35 PM
#2
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
-
Sep 23rd, 2006, 10:08 PM
#3
Thread Starter
Frenzied Member
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.
-
Sep 23rd, 2006, 10:20 PM
#4
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:
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
-
Sep 26th, 2006, 01:30 PM
#5
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|