Show some sheets in Custom xml ribbon combobox

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,935
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a custom ribbon with a combobox.

This combobox shows the names of all the worksheets within my workbook.

I have taken the code from here:

Code:
https://stackoverflow.com/questions/61824181/fill-in-a-custom-ribbon-combobox-runtime


xml code

Code:
<!--RibbonX Visual Designer 2.33 for Microsoft Excel CustomUI14 . XML Code produced on 2020/05/16-->
<customUI
    xmlns = "http://schemas.microsoft.com/office/2009/07/customui"
    onLoad="Initialize">
    <ribbon >
        <tabs >
            <tab
                id = "Tab1"
                label="Tab1">
                <group
                    id = "Group1"
                    label="Group1">
                    <comboBox
                        id = "Combo3"
                        Label = "MyCombobox"
                        getItemCount = "Combo3_getItemCount"
                        getItemID = "Combo3_getItemID"
                        getItemLabel = "Combo3_getItemLabel"
                        getText = "Combo3_getText"
                        onChange="Combo3_onChange"/>
                </group >
            </tab >
        </tabs >
    </ribbon >
</customUI >


vba code

Code:
Dim myRibbon As IRibbonUI

'Callback for customUI.onLoad
Sub Initialize(ribbon As IRibbonUI)
    Set myRibbon = ribbon
End Sub

'Callback for Combo3 getItemCount (called once when the combobox is invalidated)
Sub Combo3_getItemCount(control As IRibbonControl, ByRef returnedVal)
    returnedVal = 10 'the number of items for combobox
End Sub

'Callback for Combo3 getItemID (called 10 times when combobox is invalidated)
Public Sub Combo3_getItemID(control As IRibbonControl, index As Integer, ByRef id)
    id = "ComboboxItem" & index + 1
End Sub

'Callback for Combo3 getItemLabel (called 10 times when combobox is invalidated)
Sub Combo3_getItemLabel(control As IRibbonControl, index As Integer, ByRef returnedVal)
    returnedVal = "Item" & index + 1
End Sub

'Callback for Combo3 getText
Sub Combo3_getText(control As IRibbonControl, ByRef returnedVal)
    returnedVal = "" 'clears the text from the combobox
End Sub

'Callback for Combo3 onChange
Sub Combo3_onChange(control As IRibbonControl, text As String)
    MsgBox "You have chosen " & text
End Sub

Sub UpdateCombo3()
    myRibbon.InvalidateControl "Combo3" 'invalidates the cache for the combobox
End Sub

I want a slight modification.

Instead of displaying every sheet name in the combobox, I would like to have one sheet (called Secret) NOT to be displayed in the combobox.

I tried manually moving Secret to be the first sheet (or the last) and amended this Sub:

Code:
'Callback for Combo3 getItemLabel (called 10 times when combobox is invalidated)
Sub Combo3_getItemLabel(control As IRibbonControl, index As Integer, ByRef returnedVal)
    If index =0 then
        returnedVal = "Item" & index + 2
    Else   
        returnedVal = "Item" & index + 1
    End If
End Sub

but I didn't achieve the desired result.

Is it possible to only show some of the sheets?

Thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I've worked it out.

I manually moved the sheet Secret to be the first sheet, then amended the code to this:

Code:
'Callback for Combo3 getItemLabel (called 10 times when combobox is invalidated)
Sub Combo3_getItemLabel(control As IRibbonControl, index As Integer, ByRef returnedVal)
        returnedVal = "Item" & index + 2
   
End Sub
 
Upvote 0
Worked out an even better solution:

Code:
Dim Flag As Boolean ' ADDED THIS

Dim myRibbon As IRibbonUI

'Callback for customUI.onLoad
Sub Initialize(ribbon As IRibbonUI)
    Set myRibbon = ribbon
End Sub

'Callback for Combo3 getItemCount (called once when the combobox is invalidated)
Sub Combo3_getItemCount(control As IRibbonControl, ByRef returnedVal)
   
    Flag = False ' ADDED THIS

    returnedVal = 10 'the number of items for combobox
End Sub

'Callback for Combo3 getItemID (called 10 times when combobox is invalidated)
Public Sub Combo3_getItemID(control As IRibbonControl, index As Integer, ByRef id)
    id = "ComboboxItem" & index + 1
End Sub


' CHANGED THIS

'Callback for Combo3 getItemLabel (called 10 times when combobox is invalidated)
Sub Combo3_getItemLabel(control As IRibbonControl, index As Integer, ByRef returnedVal)

Select Case Worksheets(index + 1).Name
        Case "Secret"
            returnedVal = Worksheets(index + 2).Name
            SheetFlag = True
        Case Else
            Select Case SheetFlag
                Case True
                    returnedVal = Worksheets(index + 2).Name
                Case False
                    returnedVal = Worksheets(index + 1).Name           
            End Select
    End Select
End Sub

'Callback for Combo3 getText
Sub Combo3_getText(control As IRibbonControl, ByRef returnedVal)
    returnedVal = "" 'clears the text from the combobox
End Sub

'Callback for Combo3 onChange
Sub Combo3_onChange(control As IRibbonControl, text As String)
    MsgBox "You have chosen " & text
End Sub

Sub UpdateCombo3()
    myRibbon.InvalidateControl "Combo3" 'invalidates the cache for the combobox
End Sub

The advantage of this solution is the worksheet Secret can be anywhere, not neccessarily the first or last sheet in the Worksheets collection.
 
Upvote 0
Solution

Forum statistics

Threads
1,225,346
Messages
6,184,400
Members
453,230
Latest member
ProdInventory

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