-
Apr 2nd, 2011, 10:59 AM
#1
Thread Starter
Lively Member
[RESOLVED] Listbox Run Time Error 13
I have two listboxes. The 1st listbox has the numbers 1 to 10 in it. The 2nd listbox has the numbers 1.00 to 10.00 (increasing by 0.01) in it. And when I calculate these numbers everything is fine. But as soon as I increase the 1st listboxes' numbers to 100,000 (1 to 100,000) and I do a calculation using the number 100,000 I get a run time error 13.
Questions...
I shouldn't be able to do a calculation even when the numbers are low (1 to 10) because these numbers are strings and therefore you can't calculate strings, but it does, so the first question is... How is this possible?
My second question is, how can I calculate the 100,000 number without creating a run time error 13?
Many thanks.
-
Apr 2nd, 2011, 12:10 PM
#2
Re: Listbox Run Time Error 13
Could you post the code you are using to fill the listboxes, and to perform the calculations?
Are you, by any fat chance, displaying your numbers as "100,000", with the comma included? If so, that might be the cause of the problem. Try to remove the comma and then convert them to numbers and then do the calculations.
.
-
Apr 2nd, 2011, 12:47 PM
#3
Thread Starter
Lively Member
Re: Listbox Run Time Error 13
In the General Declarations section I have...
Code:
Dim secondvalue(100)
Dim thirdvalue(100)
To fill the listboxes I do...
Code:
For x = 1 To 10
z = Format(x, "##,##0.00#")
listsecond.AddItem z
Next x
For x = 1.0 To 10 Step 0.01
z = Format(x, "##,##0.00#")
listthird.AddItem z
Next x
To perform the calculations I get the numbers by doing...
Code:
secondvalue(1) = listsecond.List(listsecond.ListIndex)
thirdvalue(1) = listthird.List(listthird.ListIndex)
And then I do the calculations...
Code:
x = secondvalue(1) * (thirdvalue(1) / 100)
y = x / 365
amountvalue = y / 86400
Thanks for the reply.
-
Apr 2nd, 2011, 01:18 PM
#4
Re: Listbox Run Time Error 13
Yep definitely the commas are causing a problem. Remove them from the listbox entries before you process them.
.
-
Apr 2nd, 2011, 11:28 PM
#5
Thread Starter
Lively Member
Re: Listbox Run Time Error 13
I took out all the commas but the error still kept appearing. The error occurred at this line....
Code:
x = (secondvalue(1)) * (thirdvalue(1) / 100)
In the above code.....
x = Empty
secondvalue(1) = ""
thirdvalue(1) = "4.5"
And when I increased the 2nd listbox so that it's numbers were 1 to 100000. The same error appeared and I got the following results...
x = Empty
secondvalue(1) = ""
thirdvalue(1) = ""
So it looks like the strings are the problem. With a listbox, is there a function that will convert the string to a number? The val and int functions can't do it. So, any ideas?
-
Apr 3rd, 2011, 12:16 AM
#6
Re: Listbox Run Time Error 13
The problem is that a Null string cannot be converted to a number. For secondvalue(1) to be null either the code to set it has not been executed, or no item in listsecond was selected when the assignment was made.
-
Apr 3rd, 2011, 02:08 AM
#7
Re: Listbox Run Time Error 13
Originally Posted by Doogle
The problem is that a Null string cannot be converted to a number. For secondvalue(1) to be null either the code to set it has not been executed, or no item in listsecond was selected when the assignment was made.
Yumby, if your strings are not convertible to numbers, you will keep getting these errors. So the basic rule of thumb you should remember is the strings you are trying to use in calculations must be convertible to numbers. This means the strings cannot be empty, they cannot contain anything other than digits and a decimal symbol. You may have a positive or negative sign before the number, but that's basically all. Any commas, special characters, spaces, or empty strings are a strict NO NO.
That said, to fix the problem with empty strings, try prefixing them with "0". For e.g.
Code:
x = ("0" & secondvalue(1)) * ("0" & thirdvalue(1) / 100)
.
-
Apr 3rd, 2011, 03:28 AM
#8
Thread Starter
Lively Member
Re: Listbox Run Time Error 13
Thanks for the replies guys. Thanks to your help I seem to be getting closer to solving this problem. But it's going to take me a while. The only other information I can add is.....
When the listsecondlist has the numbers 1 to 100000 in it, and the number 1 is selected, a couple of message boxes tell me that the listindex of this listbox is 0 and that the number selected is 1. This is of course good.
But when the listsecondlist has the numbers 1 to 100000 in it, and the number 100000 is selected, a message box tells me that the listindex of this listbox is -31073. And then another message box tells me that nothing has been selected. In other words, the latter message box does not display the listbox's list item.
-
Apr 3rd, 2011, 03:54 AM
#9
Re: Listbox Run Time Error 13
The problem is that the ListIndex property of a ListBox is Integer, so if you have more than 32,767 entries (ie 0 to 32,766) it will go negative. Do you really want a list of 100,000 items for the user to select from ?
-
Apr 11th, 2011, 08:45 AM
#10
Thread Starter
Lively Member
Re: Listbox Run Time Error 13
Sorry for the late reply. Okay understood. Don't use commas and so on and don't go more than 32,767 entries. Problem solved. Thanks for the help guys.
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
|