Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tracking down "Else without if" error

Status
Not open for further replies.

cadcoke3

Programmer
Mar 19, 2007
4
US
I have some VBA code where I get an "Else without If" compile error. So, there is probably a missing,or extra END IF statement somewhere. But, I can't track it down. Is there any trick to help do this? It is not a lot of code, and I have tried searching for all occurances of "if", to no avail.

There is, however one thing which may somehow be affecting it. I use a number of If Then statements that are in the single line format. E.g.;

If Not EOF(1) Then Line Input #1, Current_Line

I know this form of the IF-THEN statment should not take an END IF statement. I could rewrite it in multi-line format, but I have A LOT Of these lines, and fear it would make tracking down an "else without if" error even harder.

In AutoCAD's visual LISP programming enviroment, where you have a ZILLION parenthesis, there is a command to jump to the next matching parenthesis. This is used to help track down missing parenthesis.

I should add that I am using my VBA code to extract some information from an AutoCAD log file. I must check each line to see if it has the information I am looking for.

Any advice?

Joe Dunfee
 



Hi,

Any time I write code, I use a TAB indent within For..Next, Do...Loop, With...End With, If...Else...End If, Select Case...End Select.

I enter the structure BEFORE I fill in the stuff in the middle...
Code:
If a = b Then

Else

End If
then I fill in the guts.
Code:
If a = b Then
   'Do somthing here
Else
   'Do somthing else here
End If
As for finding the offender, I'd TAB structure the existing code, print it, and match If...Else...End If in the same column.

CAVEAT: the compiler does always give the correct diagnosis. It COULD be something else.

Skip,

[glasses] [red][/red]
[tongue]
 

I'm a bit confused. The error is "Else without If" so you conclude you have a missing or extra "END IF" statement? So you search for "If"?

How about searching for the "Else"?

 
Hmmmm, indeed.

Also, there seems to be a bit of contradiction.
It is not a lot of code, and I have tried searching for all occurances of "if", to no avail.
Yet you mention
I have A LOT Of these lines, and fear it would make tracking down an "else without if" error even harder.
It is possible - as Zathras mentions - that you somehow got an Else into one of those.

I am with Skip 100% on putting opening and closing logic statements in first, then the guts. A bit of a tedious pain, but IMO far less than trying to find a wayward statement in a bunch of code.

The Indenter suggestion is also a good one. It could certainly help to find the offending statement. You can always make them one-liners after, although doing so puts the possibility of error back in if you don't remove all the proper parts.

Gerry
My paintings and sculpture
 
I do the indenting stuff, but of course, it is prone to human omissions. Thanks, PHV, for the link to the Indenter utility - that is exactly what I've been looking for.
---
Zathras said;
>The error is "Else without If" so you conclude you have
>a missing or extra "END IF" statement? So you search
>for "If"?

I've done several variations on the search, and tried not to make my initial posting too long. A search for "IF" will turn up both the beginning and end of an IF statment. I only have the one ELSE statement.
---
Gerry, I used the one-liners because those lines comprise about 1/4 of my code. Turning them all into 3-line If-Then statments just compounds the problem of tracking down the problem. However, that is exactly what I did last night... My "else" problem went away!

It is certainly too early for me to say that the one-liners were the cause of the problem. But, I will keep an eye out to see if this is a repeatable error.

By the way, a big thank you to John Backus, who passed away the other day at the age of 82. He developed the first high-level programming language, Fortran. Glad I don't have to track down a "001001010 without 001010010 error".

Joe Dunfee
 
I had changed them to multi-line statments and then the "Else without if" error went away. I just tried changing them back into one-liners, and the error came back.

It is looking more like there is somthing funny with using the one line IF statments.

Joe Dunfee
 
Better to not have : in an one-liner If...Then...Else instruction

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top