Help with Nested "If..Then..Else" Statment Syntax

  • Thread starter Thread starter Ken
  • Start date Start date
K

Ken

I can't seem to get my syntax right on the following
code. In playing with it, I either get the "Else without
If" or "End If without Block If" Errors.


Here is the code producing the errors:

Public Function SurveySelect(PropQltyRtg, AsStabVal) As
Double

If PropQltyRtg = 1 Or PropQltyRtg = 2 _
Then
SurveySelect = "CB Richard Ellis"
Else _
: If AsStabVal > 5000000 And PropQltyRtg >
_
2 Then SurveySelect = "Korpacz"
Else: SurveySelect = "CB Richard Ellis"
End If
End Function

What would be the proper code? I have tried Putting an
End If before the first Else, but that gave me another
error.

Thanks in advance!
Ken
 
Ken said:
I can't seem to get my syntax right on the following
code. In playing with it, I either get the "Else without
If" or "End If without Block If" Errors.


Here is the code producing the errors:

Public Function SurveySelect(PropQltyRtg, AsStabVal) As
Double

If PropQltyRtg = 1 Or PropQltyRtg = 2 _
Then
SurveySelect = "CB Richard Ellis"
Else _
: If AsStabVal > 5000000 And PropQltyRtg >
_
2 Then SurveySelect = "Korpacz"
Else: SurveySelect = "CB Richard Ellis"
End If
End Function

What would be the proper code? I have tried Putting an
End If before the first Else, but that gave me another
error.

Thanks in advance!
Ken

Take out the ":" line separators and format your code to clarify the
block structure of your Ifs:

If PropQltyRtg = 1 Or PropQltyRtg = 2 Then
SurveySelect = "CB Richard Ellis"
Else
If AsStabVal > 5000000 And PropQltyRtg > 2 Then
SurveySelect = "Korpacz"
Else
SurveySelect = "CB Richard Ellis"
End If
End If
 
Dick,

Thanks! That worked. Regarding Block Structures, I
thought indenting didn't matter as far as the code read,
but did as far as someone trying to comprehend it.
However, I sometimes got the "End If without Block If"
error message that means that the indetation must matter.
Is this true?

Ken
 
Ken said:
Dick,

Thanks! That worked. Regarding Block Structures, I
thought indenting didn't matter as far as the code read,
but did as far as someone trying to comprehend it.
However, I sometimes got the "End If without Block If"
error message that means that the indetation must matter.
Is this true?

Indentation does not matter.
 
Ken said:
Dick,

Thanks! That worked. Regarding Block Structures, I
thought indenting didn't matter as far as the code read,
but did as far as someone trying to comprehend it.
However, I sometimes got the "End If without Block If"
error message that means that the indetation must matter.
Is this true?

No, indentation doesn't matter to the compiler, but it may matter to
*you* when you're writing your code. In this case, you appear to have
confused yourself by using the line-separator, effectively converting
what you thought of as a "one-line If" to a "Block If".
 
If PropQltyRtg = 1 Or PropQltyRtg = 2 Then
SurveySelect = "CB Richard Ellis"
Else
If AsStabVal > 5000000 And PropQltyRtg > 2 Then
SurveySelect = "Korpacz"
Else
SurveySelect = "CB Richard Ellis"
End If
End If

Can I vote for

SurveySelect = IIf(PropQltyRtg > 2 And AsStabVal > 5E6, _
"Korpacz", _
"CB Richard Ellis")


which is the same thing?


B Wishes


Tim F
 
Tim Ferguson said:
Can I vote for

SurveySelect = IIf(PropQltyRtg > 2 And AsStabVal > 5E6, _
"Korpacz", _
"CB Richard Ellis")


which is the same thing?

<G> You're right, it does resolve to that, which I hadn't noticed. I
don't the use of exponential notation, though, and I suspect (without
knowing) that calling the IIf() function will be slower, execution-wise,
than just writing

If AsStabVal > 5000000 And PropQltyRtg > 2 Then
SurveySelect = "Korpacz"
Else
SurveySelect = "CB Richard Ellis"
End If
 
Back
Top