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

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
 
D

Dirk Goldgar

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
 
K

Ken

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
 
R

Rick Brandt

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.
 
D

Dirk Goldgar

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".
 
T

Tim Ferguson

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
 
D

Dirk Goldgar

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top