ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   End Select without Select Case, Block If without End If errors (https://www.excelbanter.com/excel-programming/377473-end-select-without-select-case-block-if-without-end-if-errors.html)

Atreides

End Select without Select Case, Block If without End If errors
 
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

NickHK

End Select without Select Case, Block If without End If errors
 
Count the "If"s and "End If"s here :

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

Seems like you want:
Case 1
If neighbours = liveMin Then
ConwaysStepCell = 1
Else
ConwaysStepCell = 0
End If

NickHK

"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




Atreides

End Select without Select Case, Block If without End If errors
 
Actually, what I wanted was:

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

I didn't realise the EndIf was required (still a VBA newbie). However, I
think it's confusing that it picked up on the Select Case rather than the End
If. I think someone suggested this a change to this to Microsoft recently.

Thanks
Atreides


"NickHK" wrote:

Count the "If"s and "End If"s here :

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

Seems like you want:
Case 1
If neighbours = liveMin Then
ConwaysStepCell = 1
Else
ConwaysStepCell = 0
End If

NickHK

"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





Bob Phillips

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




Erik Klausen

End Select without Select Case, Block If without End If errors
 
?B?QXRyZWlkZXM=?= <atreides1AThotmailD0Tcom wrote in
:

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 **** This If needs an End If
ConwaysStepCell = 0
If neighbours liveMax Then
ConwaysStepCell = 0
Else
ConwaysStepCell = 1
End If
End Select
End Function



Kind regards,
Erik Klausen

NickHK

End Select without Select Case, Block If without End If errors
 
Bob,
You have more patience that me when it comes to giving a full explanation.

NickHK

"Bob Phillips" wrote in message
...
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






NickHK

End Select without Select Case, Block If without End If errors
 
See Bob's reply for a complete explanation.

NickHK

"Atreides" <atreides1AThotmailD0Tcom wrote in message
...
Actually, what I wanted was:

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

I didn't realise the EndIf was required (still a VBA newbie). However, I
think it's confusing that it picked up on the Select Case rather than the

End
If. I think someone suggested this a change to this to Microsoft recently.

Thanks
Atreides


"NickHK" wrote:

Count the "If"s and "End If"s here :

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

Seems like you want:
Case 1
If neighbours = liveMin Then
ConwaysStepCell = 1
Else
ConwaysStepCell = 0
End If

NickHK

"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







Atreides

End Select without Select Case, Block If without End If errors
 
Hi guys,

Thanks for the tip on including the End If, but I'm still not sure about
your logic in reducing the function down. I think you are missing the fact
that neighbours could be = liveMin or liveMax or in between. Eg with liveMin
= 2, liveMax = 3

If neighbours < 2 Then
ConwaysStepCell = 0
ElseIf neighbours 3 Then
ConwaysStepCell = 0
Else
ConwaysStepCell = 1
End If

Here, if neighbours is
0, ConwaysStepCell = 0
1, ConwaysStepCell = 0
2, ConwaysStepCell = 1
3, ConwaysStepCell = 1
4, ConwaysStepCell = 0

With your example:

Select Case True
Case neighbours < 2: ConwaysStepCell = 0
Case neighbours 3: ConwaysStepCell = 0
Case neighbours = 3: ConwaysStepCell = 1
End Select

Here, if neighbours is
0, ConwaysStepCell = 0
1, ConwaysStepCell = 0
2, ConwaysStepCell = NO CASE!
3, ConwaysStepCell = 1
4, ConwaysStepCell = 0

See what I mean?

But in any case, thanks for your time in explaining different ways of
writing the function. They will help me learn different ways of inputting
functions in the future.

Atreides




"Bob Phillips" wrote:

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





Bob Phillips

End Select without Select Case, Block If without End If errors
 
.... sometimes <G

Bob


"NickHK" wrote in message
...
Bob,
You have more patience that me when it comes to giving a full explanation.

NickHK

"Bob Phillips" wrote in message
...
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








Bob Phillips

End Select without Select Case, Block If without End If errors
 
You are right, I missed that there were two variables liveMin and liveMax. I
think Nick also missed that fact. On that basis ElseIf is best, or a nested
select case, which is what I would do, as I suggested

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

which actually clearly shows the two comparisons (but I still missed it
<G). This construct shows an interesting variation on Select ... Case,
namely basing the Select on True instead of some object or variable.
--

HTH

Bob Phillips

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

"Atreides" <atreides1AThotmailD0Tcom wrote in message
...
Hi guys,

Thanks for the tip on including the End If, but I'm still not sure about
your logic in reducing the function down. I think you are missing the fact
that neighbours could be = liveMin or liveMax or in between. Eg with

liveMin
= 2, liveMax = 3

If neighbours < 2 Then
ConwaysStepCell = 0
ElseIf neighbours 3 Then
ConwaysStepCell = 0
Else
ConwaysStepCell = 1
End If

Here, if neighbours is
0, ConwaysStepCell = 0
1, ConwaysStepCell = 0
2, ConwaysStepCell = 1
3, ConwaysStepCell = 1
4, ConwaysStepCell = 0

With your example:

Select Case True
Case neighbours < 2: ConwaysStepCell = 0
Case neighbours 3: ConwaysStepCell = 0
Case neighbours = 3: ConwaysStepCell = 1
End Select

Here, if neighbours is
0, ConwaysStepCell = 0
1, ConwaysStepCell = 0
2, ConwaysStepCell = NO CASE!
3, ConwaysStepCell = 1
4, ConwaysStepCell = 0

See what I mean?

But in any case, thanks for your time in explaining different ways of
writing the function. They will help me learn different ways of inputting
functions in the future.

Atreides




"Bob Phillips" wrote:

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







NickHK

End Select without Select Case, Block If without End If errors
 
Oops, seems we missed the liveMin/liveMax difference.
Only you can tell if the logic here gives the correct value for
ConwaysStepCell for all values of liveMin and liveMax with liveMin being the
most important comparison, irrespective of liveMax.

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

'Or
If (neighbours = liveMin) and (neighbours <= liveMax) Then
ConwaysStepCell = 1
Else
ConwaysStepCell = 0
End If

The top version is probably faster as you only need a maximum of 2
comparisons, whilst the 2nd always need 3 comparisons.

NickHK

"Atreides" <atreides1AThotmailD0Tcom wrote in message
...
Hi guys,

Thanks for the tip on including the End If, but I'm still not sure about
your logic in reducing the function down. I think you are missing the fact
that neighbours could be = liveMin or liveMax or in between. Eg with

liveMin
= 2, liveMax = 3

If neighbours < 2 Then
ConwaysStepCell = 0
ElseIf neighbours 3 Then
ConwaysStepCell = 0
Else
ConwaysStepCell = 1
End If

Here, if neighbours is
0, ConwaysStepCell = 0
1, ConwaysStepCell = 0
2, ConwaysStepCell = 1
3, ConwaysStepCell = 1
4, ConwaysStepCell = 0

With your example:

Select Case True
Case neighbours < 2: ConwaysStepCell = 0
Case neighbours 3: ConwaysStepCell = 0
Case neighbours = 3: ConwaysStepCell = 1
End Select

Here, if neighbours is
0, ConwaysStepCell = 0
1, ConwaysStepCell = 0
2, ConwaysStepCell = NO CASE!
3, ConwaysStepCell = 1
4, ConwaysStepCell = 0

See what I mean?

But in any case, thanks for your time in explaining different ways of
writing the function. They will help me learn different ways of inputting
functions in the future.

Atreides




"Bob Phillips" wrote:

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







Dana DeLouis

End Select without Select Case, Block If without End If errors
 
Would something like this be ok?

Function ConwaysStepCell(Neighbours, Current, NewLife, LiveMin, LiveMax)
Select Case Current
Case 0
ConwaysStepCell = -(Neighbours = NewLife)
Case 1
ConwaysStepCell = -((Neighbours = LiveMin) And (Neighbours <= LiveMax))
Case Else
'Not sure
End Select
End Function

The idea in vba being
?-True
1

--
HTH :)
Dana DeLouis
Windows XP & Office 2003


"Atreides" <atreides1AThotmailD0Tcom wrote in message
...
Hi guys,

Thanks for the tip on including the End If, but I'm still not sure about
your logic in reducing the function down. I think you are missing the fact
that neighbours could be = liveMin or liveMax or in between. Eg with
liveMin
= 2, liveMax = 3

If neighbours < 2 Then
ConwaysStepCell = 0
ElseIf neighbours 3 Then
ConwaysStepCell = 0
Else
ConwaysStepCell = 1
End If

Here, if neighbours is
0, ConwaysStepCell = 0
1, ConwaysStepCell = 0
2, ConwaysStepCell = 1
3, ConwaysStepCell = 1
4, ConwaysStepCell = 0

With your example:

Select Case True
Case neighbours < 2: ConwaysStepCell = 0
Case neighbours 3: ConwaysStepCell = 0
Case neighbours = 3: ConwaysStepCell = 1
End Select

Here, if neighbours is
0, ConwaysStepCell = 0
1, ConwaysStepCell = 0
2, ConwaysStepCell = NO CASE!
3, ConwaysStepCell = 1
4, ConwaysStepCell = 0

See what I mean?

But in any case, thanks for your time in explaining different ways of
writing the function. They will help me learn different ways of inputting
functions in the future.

Atreides




"Bob Phillips" wrote:

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







Tushar Mehta

End Select without Select Case, Block If without End If errors
 
This is a classic case for the IIF function.

Option Explicit

Function ConwaysStepCell(neighbours, current, newLife, liveMin, liveMax)
Select Case current
Case 0
ConwaysStepCell = IIf(neighbours = newLife, 1, 0)
Case 1
ConwaysStepCell = _
IIf(neighbours < liveMin Or neighbours liveMax, 0, 1)
End Select
End Function

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article , ?B?
QXRyZWlkZXM=?= <atreides1AThotmailD0Tcom says...
Hi guys,

Thanks for the tip on including the End If, but I'm still not sure about
your logic in reducing the function down. I think you are missing the fact
that neighbours could be = liveMin or liveMax or in between. Eg with liveMin
= 2, liveMax = 3

If neighbours < 2 Then
ConwaysStepCell = 0
ElseIf neighbours 3 Then
ConwaysStepCell = 0
Else
ConwaysStepCell = 1
End If

Here, if neighbours is
0, ConwaysStepCell = 0
1, ConwaysStepCell = 0
2, ConwaysStepCell = 1
3, ConwaysStepCell = 1
4, ConwaysStepCell = 0

With your example:

Select Case True
Case neighbours < 2: ConwaysStepCell = 0
Case neighbours 3: ConwaysStepCell = 0
Case neighbours = 3: ConwaysStepCell = 1
End Select

Here, if neighbours is
0, ConwaysStepCell = 0
1, ConwaysStepCell = 0
2, ConwaysStepCell = NO CASE!
3, ConwaysStepCell = 1
4, ConwaysStepCell = 0

See what I mean?

But in any case, thanks for your time in explaining different ways of
writing the function. They will help me learn different ways of inputting
functions in the future.

Atreides




"Bob Phillips" wrote:

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







All times are GMT +1. The time now is 06:52 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com