Obtaining Ribbon ComboBox Control String Value

BrianExcel

Well-known Member
Joined
Apr 21, 2010
Messages
975
I have a custom Ribbon tab that contains a ComboBox. I used CustomUI editor to create it. And the ribbon control populates and works fine within Excel.

I am now trying to obtain the item selected, essentially so I can run a "select case" on that value and run different codes depending what is selected.

I suppose if you were to do this to a ComboBox within VBA, it would be similar to obtaining the ComboBox.Value.

I have found some code online for the ComboBox that will return the index #, but I prefer to use the name string.

Any thoughts how I could get this to work?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I found an additional page that seems to describe what I am looking for, but nothing happens when I run the following code:

Code:
'Definition of GetTheSelectedItemInDropDown which gets the selected item of the dropdown

Sub GetTheSelectedItemInDropDown(control As IRibbonControl, id As String, index As Integer)
Dim ItemName As String


    If control.id = "cbx1" Then
        ItemName = id
        MsgBox ItemName
    End If
    
End Sub


'Definition for SetTheSelectedItemInDropDown which sets the value in the dropdown from the global variable


Sub SetTheSelectedItemInDropDown(control As IRibbonControl, index As Integer, ByRef returnedVal)
Dim ItemName As String


    If control.id = "cbx1" Then
            returnedVal = ItemName
            MsgBox returnedVal
    End If


End Sub


I get no errors on the code, but nothing happens. No message boxes, etc.

Also, when I work in the CustomUI Editor, it will not let me add an "onAction" command to the combobox - it DOES generate an error stating "The 'onAction' attribute is not declared".

I am thinking perhaps I am simply just not triggering any action that's calling the subroutine, but I am not sure what the proper "action command" is.
 
Upvote 0
Try onChange instead. So, for example...

Code:
onChange="Combo1_Change"

Then your VBA call back would be...

Code:
[COLOR=green]'Callback for Combo1 onChange[/COLOR]
[COLOR=darkblue]Sub[/COLOR] Combo1_Change(control [COLOR=darkblue]As[/COLOR] IRibbonControl, text [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR])
    MsgBox text
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Hope this helps!
 
Last edited:
Upvote 0
Thanks Dominic - I was able to get a dropdown to work as effectively as the combo box, and the drpdown does accept the onAction command.

So effectively, I just used dropdown and then got all the other code to work. Literally got this working about 10 seconds before checking this thread again.

Thanks for the help!
 
Upvote 0
Glad you found a solution that works for you.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

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