VBA help needed

juster21

Well-known Member
Joined
Jun 3, 2005
Messages
867
I am trying to use a "Select...Case" statement but am missing something.
There is a combo box that the user will choose either Medium, High, or Urgent. Below is my code:
Code:
Private Sub cboPriorityLevel_Change()
    Select Case True
        Case "Urgent"
            If cboPriorityLevel.Value = "Urgent" Then
            cboPriorityLevel.BackColor = vbRed
        Case "High"
            If cboPriorityLevel.Value = "High" Then
            cboPriorityLevel.BackColor = vbOrange
        Case "Medium"
            If cboPriorityLevel.Value = "Medium" Then
            cboPriorityLevel.BackColor = vbBlue
    End Select
End Sub

I get a "Case without Select Case" compile error. What am I doing wrong?
Thanks!!!!!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Not tested, but:

<font face=tahoma><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> cboPriorityLevel_Change()
    <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> cboPriorityLevel.Text
        <SPAN style="color:#00007F">Case</SPAN> "Urgent"
            cboPriorityLevel.BackColor = vbRed
        <SPAN style="color:#00007F">Case</SPAN> "High"
            cboPriorityLevel.BackColor = vbOrange
        <SPAN style="color:#00007F">Case</SPAN> "Medium"
            cboPriorityLevel.BackColor = vbBlue
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

In the Select Case statement, you're predefining the "case" that you're checking, so that's where the cboPriorityLevel should be, as that's what's being evaluated. As opposed to trying to integrate an IF statement.

HTH,

Smitty
 
Upvote 0
You don't have an "End If" for any of the 3 If statments. Need to have those if you don't have the entire statement on one line--one "End If" per "If...then"
 
Upvote 0
So, no "If" statements are needed, if you have them you have to use "End If" statements unless your "Then" action is on the same line as the If statement, your "select" comparison was checking the value of "True" which is always "true", and finally, there is no "vbOrange" constant (at least on my machine). So this should work (i tested):

Code:
Private Sub cboPriorityLevel_Change()
    Select Case cboPriorityLevel.Value
        Case "Urgent"
            cboPriorityLevel.BackColor = vbRed
        Case "High"
            cboPriorityLevel.BackColor = RGB(255, 150, 0)
        Case "Medium"
            cboPriorityLevel.BackColor = vbBlue
    End Select
End Sub
 
Upvote 0
Smitty,
That's what I was missing. I think I was thinking I would keep throwing code at it and that would fix it. Select statements have always caused me fits but this helped alot. Thanks!!
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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