Select Case Possible?

ryancgarrett

Board Regular
Joined
Jun 18, 2011
Messages
122
Sorry I have so many posts, I'm just a noob with a big project to finish lol. So I'm pretty sure I can use Select Case for what I want to do, I'm just unsure of how to do it.

I have a form with a listbox and 5 option buttons. Each of the option buttons needs to populate the listbox with different values. On another sheet, I have the list of values to populate the listbox. These values are in column B, and in column A I have numbers next to each item, 100-600. Basically, if I click option 1 on my form I need all the values in column B that have the number 100-199 next to them in column A to go in the listbox. If option 2 is selected, 200-299 needs to go in the listbox etc.

How do I do this?!?!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi, don't feel sorry for so many posts.
If you are having trouble with a problem, other people will too :P
So, ask as many as questions you'd like on mrexcel forum cuz it's a great help for me as well.

when you mean option buttons, do you mean command buttons? (just making sure)
In that case,
Let's say your commandbutton for option 1 name is "cmdbtn1."
and your listbox name is "lstbox."

Then your code would look like
Code:
Private Sub cmdbtn1_Click()
    Application.ScreenUpdating = False

    Dim LR As Long, i As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row 'Get number of the last row
    
    For i = 1 To LR
        If Range("A" & i).Value >= 100 And Range("A" & i).Value < 200 Then
            lstbox.AddItem Range("B" & i).Value
        End If
    Next i

    Application.ScreenUpdating = True
End Sub

And if you want to clear all the items in the listbox afterward use code like
Code:
lstbox.Clear
 
Upvote 0
By option buttons I mean there are 5 little bubbles, one of which can be filled in at a time. Sorry for my lack of technicality but I don't know of any other way to describe it. From what I understand they are different than command buttons. Is the code you gave still correct?
 
Upvote 0
Ohhh I finally understand then.
Option button. I haven't used it before so my apologies xD
 
Upvote 0
Hello,
here is the revised code

If your option button name is "OptBtn1" and listbox name is "lstbox" then use
Code:
Private Sub OptBtn1_Change()
    Dim LR As Long, i As Long
    Application.ScreenUpdating = False
    If OptBtn1.Value = True Then
        
        LR = Range("A" & Rows.Count).End(xlUp).Row 'Get number of the last row
        
        For i = 1 To LR
            If Range("A" & i).Value >= 100 And Range("A" & i).Value < 200 Then
                lstbox.AddItem Range("B" & i).Value
            End If
        Next i
    End If
    Application.ScreenUpdating = True
End Sub

You must include this whole code for each of your optionbutton and replacing the names of your option code and also replacing your range of values.
I cannot think of a way where you can just do it with 1 single event as I haven't dealt with them before
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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