PDA

View Full Version : [SOLVED:] ELSE without IF Error - multiple line breaks including AND and THEN



konalion
03-21-2016, 11:41 AM
I continue to receive an ELSE without IF error for the below code. I'm sure it's some line break syntax issue, or maybe a complication of having the AND and THEN in the IF statement. Any guidance or suggestions are appreciated. I'm sure there are better ways to represent the entire code set, and I'm more than interested in those as well, but ultimately I just want the ELSE without IF error to stop. Thanks in advance for your assistance.


If r = ((DateSerial(collYears(s), collMonths(t), (1 + 7 * (collPosition(1))))) - _
(Weekday(DateSerial(collYears(s), collMonths(t), (8 - (collDY(1)))), vbSunday))) And _
((DateSerial(collYears(s), collMonths(t), (1 + 7 * (collPosition(1))))) - _
(Weekday(DateSerial(collYears(s), collMonths(t), (8 - (collDY(1)))), vbSunday))) > _
(DateSerial(collYears(s), (collMonths(t) + 1), 0)) Then _
collPrint.Add ((DateSerial(collYears(s), collMonths(t), (1 + 7 * (4)))) - _
(Weekday(DateSerial(collYears(s), collMonths(t), (8 - (collDY(1)))), vbSunday))), CStr _
(((DateSerial(collYears(s), collMonths(t), (1 + 7 * (4)))) - _
(Weekday(DateSerial(collYears(s), collMonths(t), (8 - (collDY(1)))), vbSunday))))
ElseIf r = ((DateSerial(collYears(s), collMonths(t), (1 + 7 * (collPosition(1))))) - _
(Weekday(DateSerial(collYears(s), collMonths(t), (8 - (collDY(1)))), vbSunday))) Then _
collPrint.Add r, CStr(r)

mancubus
03-21-2016, 01:15 PM
basic If - End If block



If Condition Then
'Do stuff
ElseIf Another_Condition Then
'Do Another_Stuff
ElseIf Another_Condition Then
'Do Another_Stuff
'....
'....
'....
End If

konalion
03-21-2016, 01:54 PM
Thanks for the general information mancubus, but I'm looking for something more specific to the code set I've posted, given that it's not a basic IF - END IF block. I beleive the problem is somewhere in the fact that I'm breaking across several rows of code, which I know can have an impact if not properly constructed; I have an AND statement included, which may be creating an issue preventing me from using ELSEIF the way I envisioned; or I have an issue with the construction of the break between the initial IF and ELSEIF statements. At least those are where I think the problem might be. I've been looking at it too long now to see anything, and was hoping someone might be able to see what I'm overlooking.

Thanks again

konalion
03-21-2016, 02:29 PM
I figured it out.

Because I broke the line after the THEN, it terminated the IF statement, so the ELSEIF was orphaned. Once the ELSE without IF error was cleared, I then needed to include the END IF to allow the code to continue.

Corrected code:

If r = ((DateSerial(collYears(s), collMonths(t), (1 + 7 * (collPosition(1))))) - _
(Weekday(DateSerial(collYears(s), collMonths(t), (8 - (collDY(1)))), vbSunday))) And _
((DateSerial(collYears(s), collMonths(t), (1 + 7 * (collPosition(1))))) - _
(Weekday(DateSerial(collYears(s), collMonths(t), (8 - (collDY(1)))), vbSunday))) > _
(DateSerial(collYears(s), (collMonths(t) + 1), 0)) Then
collPrint.Add ((DateSerial(collYears(s), collMonths(t), (1 + 7 * (4)))) - _
(Weekday(DateSerial(collYears(s), collMonths(t), (8 - (collDY(1)))), vbSunday))), CStr _
(((DateSerial(collYears(s), collMonths(t), (1 + 7 * (4)))) - _
(Weekday(DateSerial(collYears(s), collMonths(t), (8 - (collDY(1)))), vbSunday))))
ElseIf r = ((DateSerial(collYears(s), collMonths(t), (1 + 7 * (collPosition(1))))) - _
(Weekday(DateSerial(collYears(s), collMonths(t), (8 - (collDY(1)))), vbSunday))) Then _
collPrint.Add r, CStr(r)
End If