View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips Bob Phillips is offline
external usenet poster
 
Posts: 1,726
Default End Select without Select Case, Block If without End If errors

Nick has showed you that you don't need as many tests as you have if you
re-structure them, but it doesn't tell you why yours is wrong.

Essentially, every IF must be terminated by an End If. So

If neighbours < liveMin Then
ConwaysStepCell = 0
If neighbours liveMax Then
ConwaysStepCell = 0
Else
ConwaysStepCell = 1
End If

should be

If neighbours < liveMin Then
ConwaysStepCell = 0
End If
If neighbours liveMax Then
ConwaysStepCell = 0
Else
ConwaysStepCell = 1
End If

Or you can add the second test to the overall If

If neighbours < liveMin Then
ConwaysStepCell = 0
ElseIf neighbours liveMax Then
ConwaysStepCell = 0
Else
ConwaysStepCell = 1
End If

Or you can nest another Select ... Case

Select Case True
Case neighbours < liveMin: ConwaysStepCell = 0
Case neighbours liveMax: ConwaysStepCell = 0
Case neighbours = liveMax: ConwaysStepCell = 1
End Select

This last shows you more clearly that two conditions take the same action,
so you can simplify the test by reversing it

Select Case True
Case neighbours = liveMax: ConwaysStepCell = 1

Case Else: ConwaysStepCell = 0
End Select

which is equivalent to Nick's

If neighbours = liveMin Then
ConwaysStepCell = 1
Else
ConwaysStepCell = 0
End If

or even

ConwaysStepCell = IIf(neighbours = liveMin ,1,0)
--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)

"Atreides" <atreides1AThotmailD0Tcom wrote in message
...
I am trying to implement the function below (good computer scientists will
know why =)). However I keep getting the following errors:

"End Select without Select Case"

If I try moving the tabs around, I sometimes get the following error:

"Block If without End If"

However, these error messages don't seem to make sense as everything is
closed. How can I fix this?

Thanks
Atreides


=========
Function ConwaysStepCell(neighbours, current, newLife, liveMin, liveMax)
Select Case current
Case 0
If neighbours = newLife Then
ConwaysStepCell = 1
Else
ConwaysStepCell = 0
End If
Case 1
If neighbours < liveMin Then
ConwaysStepCell = 0
If neighbours liveMax Then
ConwaysStepCell = 0
Else
ConwaysStepCell = 1
End If
End Select
End Function