How to know which button in group was selected?

TAPS_MikeDion

Well-known Member
Joined
Aug 14, 2009
Messages
516
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:

6StringJazzer

Active Member
Joined
Jan 27, 2010
Messages
289
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
516
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,078,447
Messages
5,340,344
Members
399,370
Latest member
salamon

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top