|
|
Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
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
|
|
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
"Joanne" wrote:
[Quoted Text] > 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
|
|
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
"Joanne" wrote:
[Quoted Text] > 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
|
|
The elements will never execute. As soon as it hits the Exit For, it bypasses all other statements in the code.
"S Isfeld" wrote:
[Quoted Text] > 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 > > > "Joanne" wrote: > > > 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
|
|
=?Utf-8?B?S2xhdHV1?= <Klatuu[ at ]discussions.microsoft.com> wrote in news:3429FDEC-BAB3-486B-9CF3-71767942A37D[ at ]microsoft.com:
[Quoted Text] > 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
|
|
Syntactically correct, but IMHO, poor form. Consistent use if If then/End If is always easier to read. Easy to read avoids confusion.
"Tim Ferguson" wrote:
[Quoted Text] > =?Utf-8?B?S2xhdHV1?= <Klatuu[ at ]discussions.microsoft.com> wrote in > news:3429FDEC-BAB3-486B-9CF3-71767942A37D[ at ]microsoft.com: > > > 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 > >
|
|
|