Search code examples
excelvbacomboboxribbonx

Excel Ribbon comboBox: How to set up an onChange Callback


Setting up a comboBox in a custom Ribbon.

Don't understand, how to build the onChange Callback.

This is the comboBox XML:

<group id="GroupDemo2" 
    label="SelectSheet"
    autoScale="true"
    imageMso="AddInManager">
    <comboBox id="ComboBox001"
        label="comboBox001”
        sizeString=“XXXX”
        onChange="RibbonCallbacks.ComboBox001OnChange"
        getText="RibbonCallbacks.ComboBox001GetText">
        <item id="ItemOne”
            label=“One”/>
        <item id="ItemTwo”
            label=“Two”/>
        <item id="ItemThree”
            label=“Three”/>
    </comboBox>
</group>     

I tried this, but it doesn't do anything:

Sub ComboBox001OnChange(control As IRibbonControl, id As String)
    Select Case id
        Case "ItemOne”
            Sheets("Sheet1”).Select
        Case "ItemTwo”
            Sheets("Sheet2”).Select
        Case "ItemThree”
            Sheets("Sheet3”).Select
    End Select
End Sub

Thanks!


Solution

    • Replace fullwidth double quote (Unicode) with halfwidth char in XLM and VBA code.

    Halfwidth and fullwidth forms

    • The name of callback Sub should be ComboBox001_OnChange, a underscore is missing.

    • The return value id is One instead of ItemOne.

    -- XML
    <group id="GroupDemo2" 
        label="SelectSheet"
        imageMso="AddInManager">    
        <comboBox id="ComboBox001"
            label="comboBox001"
            onChange="ComboBox001_OnChange">
            <item id="ItemOne"
                label="One"/>
            <item id="ItemTwo"
                label="Two"/>
            <item id="ItemThree"
                label="Three"/>
        </comboBox>
    </group>   
    
    -- Callback VBA Code 
    Sub ComboBox001_OnChange(control As IRibbonControl, id As String)
        Select Case id
            Case "One"
                Sheets("Sheet1").Select
            Case "Two"
                Sheets("Sheet2").Select
            Case "Three"
                Sheets("Sheet3").Select
        End Select
    End Sub