How to know which button in group was selected?

TAPS_MikeDion

Well-known Member
Joined
Aug 14, 2009
Messages
622
Office Version
  1. 2011
Platform
  1. MacOS
Hi all,

I have three (3) button in a Group:

Group Name:
Sorting

Button Names:
FirstNameSortButton
LastNameSortButton
LocationSortButton


Since a ComboBox cannot show more than 1 column when the "ComboBox1_Change()" event fires, I had to overlay a 2 column ListBox on top of my ComboBox so all 3 columns of info can be shown (i.e. Last Name, First Name, Location.)

Depending on which button in the Group is selected will depend on what order the data is placed into the 3 columns (Column 1 = ComboBox, and Columns 2 & 3 = ListBox.) In a nutshell, I just want the "ComboBox1_Change()" event to display the three columns like it does when the "ComboBox1_Click()" event fires.

Hopefully someone can assist, and hopefully I didn't lose you with what I need to accomplish.

Here's what I was trying, but the "Application.Caller" (something I came across in a search) doesn't like my code. I get a Type Mismatch in the Case clause, and I'm not even sure if I can run a Select Case statement within a With statement since the Run-Time Error stopped the code at the type mismatch.

Code:
Private Sub ComboBox1_Change()
        
    Dim SelectedRow As Long
    Dim sbSelected As Long
    Dim ws As Worksheet
    Set ws = Sheets("DataSheet")
    SelectedRow = Me.ComboBox1.ListIndex + 2
    
    Select Case Application.Caller
        Case FirstNameSortButton
            sbSelected = 1
        Case LastNameSortButton
            sbSelected = 2
         Case LocationSortButton
            sbSelected = 3
    End Select
    
    With Me.ListBox1
        .Clear
        .ColumnHeads = False
        .ColumnCount = 2
        .ColumnWidths = "186;186"
        .Font.Size = 14
        Select Case sbSelected
            Case 1
                'FirstNameButton selected
                'Show Last Name & Location
                .AddItem ""
                .List(0, 0) = ws.Cells(SelectedRow, 2)
                .AddItem ""
                .List(0, 1) = ws.Cells(SelectedRow, 28)
             Case 2
                'LastNameButton selected
                'Show First Name & Location
                .AddItem ""
                .List(0, 0) = ws.Cells(SelectedRow, 3)
                .AddItem ""
                .List(0, 1) = ws.Cells(SelectedRow, 28)
             Case 3
                'LocationButton selected
                'Show First Name & Last Name
                .AddItem ""
                .List(0, 0) = ws.Cells(SelectedRow, 3)
                .AddItem ""
                .List(0, 1) = ws.Cells(SelectedRow, 2)
      End Select
      
   End With
   
End Sub

As always, thank you!
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I am a little unclear as to what order the user is doing this. I'm going to assume these are optionbuttons and the user first selects an optionbutton then selects from the combobox.

Code:
Private Sub ComboBox1_Change()
        
    Dim SelectedRow As Long
    Dim sbSelected As Long
    Dim ws As Worksheet
    Set ws = Sheets("DataSheet")
    SelectedRow = Me.ComboBox1.ListIndex + 2
    
[COLOR=#FF0000]    If FirstNameSortButton Then[/COLOR]
[COLOR=#FF0000]        sbSelected = 1[/COLOR]
[COLOR=#FF0000]    ElseIf LastNameSortButton Then[/COLOR]
[COLOR=#FF0000]        sbSelected = 2[/COLOR]
[COLOR=#FF0000]    ElseIf LocationSortButton Then[/COLOR]
[COLOR=#FF0000]        sbSelected = 3[/COLOR]
[COLOR=#FF0000]    End If[/COLOR]
    
    With Me.ListBox1
        .Clear
        .ColumnHeads = False
        .ColumnCount = 2
        .ColumnWidths = "186;186"
        .Font.Size = 14
        Select Case sbSelected
            Case 1
                'FirstNameButton selected
                'Show Last Name & Location
                .AddItem ""
                .List(0, 0) = ws.Cells(SelectedRow, 2)
                .AddItem ""
                .List(0, 1) = ws.Cells(SelectedRow, 28)
             Case 2
                'LastNameButton selected
                'Show First Name & Location
                .AddItem ""
                .List(0, 0) = ws.Cells(SelectedRow, 3)
                .AddItem ""
                .List(0, 1) = ws.Cells(SelectedRow, 28)
             Case 3
                'LocationButton selected
                'Show First Name & Last Name
                .AddItem ""
                .List(0, 0) = ws.Cells(SelectedRow, 3)
                .AddItem ""
                .List(0, 1) = ws.Cells(SelectedRow, 2)
      End Select
      
   End With
   
End Sub
 
Upvote 0
Correct assumption. Sorry, forgot to mention that.

It's always a head-smacker when someone else edits your code and you say to yourself...duh. :)

Thanks 6String!
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,127
Members
453,021
Latest member
Justyna P

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