View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Adrian C[_2_] Adrian C[_2_] is offline
external usenet poster
 
Posts: 21
Default optimize If conditional expression?

On 20/01/2010 03:45, Joe User wrote:
"JLGWhiz" wrote:
Well, the options appear to be using If...ElseIf...Else...End If


The "problem" is: it would have to be of the form:

If condition1 Then
Goto doit
Else If condition 2 Then
Goto doit
Else If condition3 Then
doit:
...statements...
End If

Not really a problem. Just undesirable. Also, "...statements..." cannot
be procedurized easily.


How about ...

Function test()
Dim strName As String
strName = "Mr Tibbs"

If CheckGreeting(strName) Then
MsgBox "Hello " & strName
End If

End Function

Function CheckGreeting(strName As String) As Boolean
If Left(strName, 2) = "Mr" Then
CheckGreeting = True
Else If Left(strName, 2) = "Sr" Then
CheckGreeting = True
Else
CheckGreeting = (Left(strName, 2) = "Ms")
End If
End Function


--
Adrian C