0

For the following input box, why does the "case" statement not evaluate but the "If" does? I've declared strInputVal as a string.

strInputVal = InputBox("How many payments?")

This works:

 If strInputVal = ""   
 Then GoTo Exit_sub  
 End If  

Below does not work:

Select Case IsNumeric(strInputVal)
     Case strInputVal = vbNullString
         GoTo Exit_sub
     End select
Community
  • 1
  • 1
user1863490
  • 21
  • 1
  • 2
  • 1
    your case statement is incorrect. The case statement equats the select case statement. so what your select case isnumeric(strinputval) will return either a true or a false. Those what you case statement should be looking for. case true or case false. change to select case strinputval and case vbnullstring. Se my answer below – Scott Craner Aug 27 '15 at 20:34

2 Answers2

0

the case statement should read

Select Case strInputVal
    case vbnullString
        GoTo Exit_sub
end select
Scott Craner
  • 148,073
  • 10
  • 49
  • 81
0

You have stumbled upon a unique syntax for Select Case

I think of it as Select Case True. It is an alternate to the If..else if structure.

With this syntax each case is evaluated to a Boolean and compared to the Boolean which you provide.

So doing some of the evaluations, your code is equivalent to this:

Select Case False
   Case "" = vbNullString ' skipped because not False
     GoTo Exit_Sub
End Select

I think what you meant to do is the more standard case syntax:

Select case strInputVal
  case vbEmptyString
    GoTo Exit_Sub
End Select

But you could have written it like this:

Select Case True
   Case "" = vbNullString ' not skipped, because true
     GoTo Exit_Sub
End Select
Community
  • 1
  • 1
Daniel
  • 12,982
  • 3
  • 36
  • 60