Where is the If statement going wrong?

G

Guest

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
 
G

Guest

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
 
G

Guest

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
 
G

Guest

The elements will never execute. As soon as it hits the Exit For, it
bypasses all other statements in the code.
 
T

Tim Ferguson

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

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
 
G

Guest

Syntactically correct, but IMHO, poor form. Consistent use if If then/End If
is always easier to read. Easy to read avoids confusion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top