Select Case Function

WindsorKnot

Board Regular
Joined
Jan 4, 2009
Messages
160
Hi,

I'm trying to learn how to write functions with Select Case and am having trouble with my code below. Basically, it returns 0 when any of the three plans below are selected. Basically there if for example Health - Option 1 is selected I want the function to return "High". I know how to do this using the if-then arguements but want to learn how to do this using select case. Any input would greatly appreciated.

Code:
Function plan(tier)
    Dim High As String
    Dim Mid As String
    Dim Low As String
    Dim Opt1 As String
    Dim Opt2 As String
    Dim Opt3 As String
    Opt1 = "Health - Option 1"
    Opt2 = "HDHP Lower Deductible"
    Opt3 = "HDHP High Deductible"
    Select Case tier
        Case Opt1
        plan = High
        Case Opt2
        plan = Mid
        Case Opt3
        plan = Low
        Case Else
         MsgBox "Select a valid plan"
         Exit Function
    End Select
End Function
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try plan = "High"

currently it's looking for the value of a variable called high
 
Upvote 0
Hi again

Try changing

Code:
plan = High
to
Code:
plan = "High"

Repeat for the other case lines

I haven't looked at your code properly but that's the problem that stands out.

Jason
 
Upvote 0
Hello,

if you are trying to get the plan variable to be either HIgh , mID or Low, you need to put them in quotes i.e. plan="High" etc.
 
Upvote 0
Try

Code:
Function plan(tier)
Dim Opt1 As String
Dim Opt2 As String
Dim Opt3 As String
Opt1 = "Health - Option 1"
Opt2 = "HDHP Lower Deductible"
Opt3 = "HDHP High Deductible"
Select Case tier
    Case Opt1
        plan = "High"
    Case Opt2
        plan = "Mid"
    Case Opt3
        plan = "Low"
    Case Else
        MsgBox "Select a valid plan"
    End Select
End Function
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top