PDA

View Full Version : Case in Case in If then .. else ... end if: compilation error



LtB
09-06-2012, 03:15 AM
I'm having a problem...
In a VBA reference I find

Finally, let's take a look at an example that uses Else.
If LRegion ="N" Then
LRegionName = "North"
ElseIf LRegion = "S" Then
LRegionName = "South"
ElseIf LRegion = "E" Then
LRegionName = "East"
Else
LRegionName = "West"
End If

I do not need the two elseIf parts.
When I use only if... ten..., everything is OK.
When I cntinue with else..... end if part , I get a compiler Error: "Else without If"

Between Else and End If I have two nested Case statements, like this:
If LRegion ="N" Then
correct code
Else
Select Case test_expression

Case condition_1
result_1
Case condition_2
result_2
...
Case condition_n
Select Case test_expression2
Case condition_1
result_1
Case condition_2
result_2
...
Case condition_n
result_n
End Select
End Select
End If
These case constructions compile well (also nested) when not entangled in a else.. end if

What can be the matter?
Too deep nesting level? that cannot be possible with only 3 levels...

(Excel 2007, Windows 7, both Dutch localised)

snb
09-06-2012, 03:47 AM
If LRegion ="N" Then
' no problem
Else
Select Case test_expression
Case condition_1
'result_1
Case condition_2
'result_2
Case condition_n
Select Case test_expression2
Case condition_1
'result_1
Case condition_2
'result_2
Case condition_n
'result_n
End Select
End Select
End If



In what line the problem occurs ?

Bob Phillips
09-06-2012, 03:55 AM
Also why have a do-nothing test, just use

If LRegion <> "N" Then

Select Case test_expression

LtB
09-06-2012, 05:02 AM
In what line the problem occurs ?
The two case constructs were ready, working all right.
Then I allen the If.. Then... construction.
Still Ok.
Added Else
left the line to add more, and immediately the error came up. That was logical, I had not finished the construction.
When I put the Endif in place, the message still was there.

LtB
09-06-2012, 05:13 AM
Also why have a do-nothing test, just use

If LRegion <> "N" Then

Select Case test_expression
It was the Else psart that played havoc.

But It was a good idea.
I commented out the End If, and added End If straight after Else.

The Editor added ":" after Else! (I always forget how ":" is called in English: a colon?)

I could move End If back to where it belongs, and no more errors.

Remaining Question:
IS THIS USE OF ":" after Else documented somewhere?
I did not find it in the VB Help...

Bob Phillips
09-06-2012, 05:19 AM
It is usually a multi-statement separator, or a label close.

snb
09-06-2012, 05:42 AM
You could consider it to be an 'Enter' (next line)