Hello, I have the following code which keeps telling me that I don't have an
"End If" defined, but I have the right amount of Ifs and End Ifs. I think
the problem is in the "Exit For" statement because if I remove it and do a
regular If test then it doesn't error out. Am I doing something wrong with
the Exit For? It's only the second time I've used it. Thank you in advance
for your help.
If Len(Trim(vstrSearch)) > 0 Then
If vstrResult > 0 Then
straryElement = Split(vstrSearch, ",")
intFinalNumber = UBound(straryElement)
End If
For intCounter = 0 To intFinalNumber
'ReDim Preserve straryAllElements(1, intLast)
intPos = InStr(straryElement(intCounter), ",")
If intPos = 0 Then Exit For
strArayAllElements(0, intFinalNumber) =
Left(straryElement(intCounter), intPos - 1)
strArayAllElements(1, intFinalNumber) =
Mid(straryElement(intCounter), intPos + 1)
'intLast = intLast + 1
End If
Next intCounter
End If
S Isfeld - 25 Sep 2006 18:15 GMT
When creating an if statement if you enter somthing after the then it treats
it as a single line if no end if is used.
Should look like:
If intPos = 0 Then
Exit For
strArayAllElements(0, intFinalNumber) =
Left(straryElement(intCounter), intPos - 1)
strArayAllElements(1, intFinalNumber) =
Mid(straryElement(intCounter), intPos + 1)
'intLast = intLast + 1
End If
Assuming you wanted the strArayAllElements to execute only if intPos=0
otherwise move them to after the end if
> Hello, I have the following code which keeps telling me that I don't have an
> "End If" defined, but I have the right amount of Ifs and End Ifs. I think
[quoted text clipped - 22 lines]
>
> End If
Klatuu - 25 Sep 2006 20:07 GMT
The elements will never execute. As soon as it hits the Exit For, it
bypasses all other statements in the code.
> When creating an if statement if you enter somthing after the then it treats
> it as a single line if no end if is used.
[quoted text clipped - 39 lines]
> >
> > End If
Klatuu - 25 Sep 2006 18:20 GMT
The problem is here:
If intPos = 0 Then Exit For
strArayAllElements(0, intFinalNumber) =
Left(straryElement(intCounter), intPos - 1)
strArayAllElements(1, intFinalNumber) =
Mid(straryElement(intCounter), intPos + 1)
'intLast = intLast + 1
End If
Unless you are doing a one line If (which, IMHO, is never good), there
should be nothing after the word Then.
Now, even if you make that fix, you need an Else:
If intPos = 0 Then
Exit For
Else
strArayAllElements(0, intFinalNumber) =
Left(straryElement(intCounter), intPos - 1)
strArayAllElements(1, intFinalNumber) =
Mid(straryElement(intCounter), intPos + 1)
'intLast = intLast + 1
End If
> Hello, I have the following code which keeps telling me that I don't have an
> "End If" defined, but I have the right amount of Ifs and End Ifs. I think
[quoted text clipped - 22 lines]
>
> End If
Tim Ferguson - 26 Sep 2006 19:37 GMT
> If intPos = 0 Then
> Exit For
[quoted text clipped - 5 lines]
> 'intLast = intLast + 1
> End If
this is just the same as
If intPos = 0 Then Exit For
strArayEct = etc
strArayEtc = etc
with no EndIf at all.
Just a thought
Tim F
Klatuu - 26 Sep 2006 19:48 GMT
Syntactically correct, but IMHO, poor form. Consistent use if If then/End If
is always easier to read. Easy to read avoids confusion.
> > If intPos = 0 Then
> > Exit For
[quoted text clipped - 17 lines]
>
> Tim F