user3323922
user3323922

Reputation: 205

Case Statement in VBA

Case statement below is not working when the condition met.

Dim TemplatePick As String

Select Case TemplatePick

Case OptCreate.Value = True
Call WebFormInfo

Case OptModify = True
Call ModifyTemplate

' many more case statement to come 
End Select

Upvotes: 2

Views: 2525

Answers (2)

Gratzy
Gratzy

Reputation: 9389

Your case test expression (TemplatePick) is not the same as your expresion list (OptCreate.Value, OptMOdify). I have a hard time even understanding what your are trying to do. Properly structured it would look something like this:

Dim TemplatePick As String

Select Case TemplatePick

Case "Template 1"
 Call WebFormInfo
Case "Template 2"
 Call ModifyTemplate
...
case Else
  'Do default behavior
End Select

More resources https://msdn.microsoft.com/en-us/library/cy37t14y.aspx

Upvotes: 3

Scott Craner
Scott Craner

Reputation: 152505

Select case is used to test the one value in this case TemplatePick then the Case would be Case "A" which would fire when TemplatePick = "A"

So for this to work:

Select Case True

Case OptCreate.Value
Call WebFormInfo

Case OptModify 
Call ModifyTemplate

' many more case statement to come 
End Select

Now one caveat with Select Case, once it finds a match it ignores all others. In other words if OptCreate.Value is True then it will stop and not test whether OptModify is True.

Upvotes: 3

Related Questions