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:

6StringJazzer

Active Member
Joined
Jan 27, 2010
Messages
370
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,081,476
Messages
5,358,908
Members
400,514
Latest member
JoHio2577

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top