quote:
Originally posted by Matty
InStr() needs 3 parameters:
InStr(CharToStartAt,String,TextToFind)
Nope, the first paramater is optional though...
If omitted it will always be 1.
----------------------
quote:
Originally posted by DJeX
Can any one help?
It would have been better if you said on what line that error occurs. And that you provided more code (eg: what are the declarations, what is s used for, etc.. etc..)
Anyways, with the info given:
- I hope you used Option Explicit at the beginning of each module, form and/or class
- Always explicitly use the property name you want to use. Never relay on default property names:
Don 't use: s = List1
but use: s = List1.Text
- Why using the "s" variable in that way?
a) This:
s = ""
s = s + Mid$(List1, InStrRev(List1, "-") + 2)
s = Replace(s, "http://", "")
can be much shorter and faster by doing:
s = Replace(Mid$(List1, InStrRev(List1, "-") + 2), "http://", "")
a) What are actually trying to do with that string manipulation? (because at first sight, it seems a bit dodgy (no offense) eg: what if the url itself contains a "-"? What if the whole string doesn't contain a "-"?)
c) What is the use in calculating/creating that "s" variable anyways; It isn't used anywhere and you also always set it to an empty string first (so it can't be a global kind of stringlist either)?
- If you need to parse thru a list, never use the .ListIndex property. This is dead, extremely dead slow...
Use the .List() property for that. No need for resetting and updating the listindex at all, it is completely useless, makes bigger code and is very slow. Only do what you did when you explicitly need a visual update on each line (but even then your code will not work as it is supposed because Windows will not update the control on screen as long as it don't have th time to do it (not being idle))
The .List() property is an array containing all the data from the list starting at index 0 to ListCount-1.
Thus instead of:
List1.ListIndex = 0
For a = 0 To List1.ListCount - 1
If InStr(List1.Text, websitein2) Then
s = blahblah
Else
List1.ListIndex = List1.ListIndex + 1
End If
Next a
Do:
For a = 0 To List1.ListCount - 1
If InStr(List1.List(a), websitein2) Then
s = blahblah
End If
Next a
- Finally, and to come to your actual problem:
a) What is "websitein2". Is it defined as a string?
b) What if your list doesn't not contain any elements? Setting ListIndex to 0 will not work then and will produce an error.
c) If you come to the end of your loop, thus with "a" being equal to listcount-1, there is a big possebility that you set the listindex to one element higher than what the list contains. If that happens an error will occur:
If ListCount = 5 (meaning there are 5 elements) and "a" is 4 (index of 5th element):
For a = 0 To List1.ListCount - 1
If InStr(List1, websitein2) Then
s = ""
s = s + Mid$(List1, InStrRev(List1, "-") + 2)
s = Replace(s, "http://", "")
Else
List1.ListIndex = List1.ListIndex + 1
' ListIndex can not be set higher than the index of the last element in the list!!!
' This is what gives you that error.
' But don't try to fix this with ways like If Then Else structures.
' Use the method described in point 4 instead
End If
Next a
----------------------