How to know which button in group was selected?

TAPS_MikeDion

Well-known Member
Joined
Aug 14, 2009
Messages
524
Office Version
2011
Platform
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:

Some videos you may like

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

6StringJazzer

Active Member
Joined
Jan 27, 2010
Messages
449
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
 

TAPS_MikeDion

Well-known Member
Joined
Aug 14, 2009
Messages
524
Office Version
2011
Platform
MacOS
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!
 

Forum statistics

Threads
1,089,453
Messages
5,408,317
Members
403,196
Latest member
annph

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top