Vba 如何在自定义下拉功能区控件上设置选定项

Vba 如何在自定义下拉功能区控件上设置选定项,vba,excel,ribbon,ribbonx,Vba,Excel,Ribbon,Ribbonx,我正在用自定义UI编辑器为Excel创建一个自定义选项卡,其中有两个下拉控件。让我们把它们叫做DropDown1和DropDown2。我的目标是,每当我更改DropDown1选择时,它都会自动更改de DropDown2选择,但我不知道如何在下拉控件中设置“SelectedItem” 到目前为止,我有一个VBA函数,每当我更改DropDown1的选择时,它都会被触发。我认为这会很有帮助。您需要在自定义UI编辑器中为功能区XML添加回调函数,然后将相应的代码添加到VBA项目中,以便在功能区选项卡失

我正在用自定义UI编辑器为Excel创建一个自定义选项卡,其中有两个下拉控件。让我们把它们叫做DropDown1和DropDown2。我的目标是,每当我更改DropDown1选择时,它都会自动更改de DropDown2选择,但我不知道如何在下拉控件中设置“SelectedItem”


到目前为止,我有一个VBA函数,每当我更改DropDown1的选择时,它都会被触发。我认为这会很有帮助。

您需要在自定义UI编辑器中为功能区XML添加回调函数,然后将相应的代码添加到VBA项目中,以便在功能区选项卡失效时调用。您需要为下拉控件设置所选项的回调是
GetSelectEditEmidex
getSelectedItemID
,具体取决于您是要按索引还是按id选择项。由于您未提供任何代码,因此我的示例是通用的(未测试):

功能区XML:

编辑

基于其他水滴列表选择索引的示例。在类似的解决方案中,我在一个控件的
onAction
函数中设置了一个值,并使用它在另一个控件中设置所选索引,如下所示:

功能区XML:


这些是数据验证下拉列表还是activeX组合框?您应该发布一些代码,这样就不会有混淆。@Amorpherus它是一个添加在Excel功能区中的下拉控件。这就是目标:谢谢奥利!但我还是不懂“怎么做”。因此,我有两个下拉列表(据我所知),我将有两个“getSelectedItemIndex”函数。当下拉选择更改时,将调用这些函数。因此,在我的“DropDown1”的“GetSelectEditedIndex”函数中,需要一些代码来设置“DropDown2”的SelectedItem。我还是不知道怎么做。如果您能提供帮助,我将不胜感激。
getSelectedItemIndex
将在功能区无效且需要重新绘制时调用,因此您可以使用它来决定将选择哪个索引。你如何决定哪个索引取决于你自己。我将在我的答案中添加一个例子。我已经找了很长时间了。。。谢谢你,奥利@如果你能在这方面帮助我,请告诉我。我一直在为类似的事情挣扎!希望这不违反社区规则@OlleSjögren我的问题贴在这里:-如果你有机会看看。
<dropDown id="drpTest" label="Test dropdown" getSelectedItemIndex="drpTestGetSelectedItem" ></dropDown>
'Callback for drpTest getSelectedItemIndex
Sub drpTestGetSelectedItem(control As IRibbonControl, ByRef returnedVal)
    returnedVal = 1   '***** To select the item with index 1,
                      '***** replace with code to select the desired item
End Sub
<dropDown id="drpTest1" label="Test dropdown 1" onAction="drpTest1OnAction" ></dropDown>
<dropDown id="drpTest2" label="Test dropdown 2" getSelectedItemIndex="drpTest2GetSelectedItem" ></dropDown>
Global myRibbon As IRibbonUI
Global giIndex As Integer

'Callback for customUI.onLoad
Sub RibbonOnLoad(ribbon As IRibbonUI)
    '***** Save reference to ribbon object to invalidate
    Set myRibbon = ribbon
End Sub

'Callback for drpTest1 onAction
Sub drpTest1OnAction(control As IRibbonControl, id As String, index As Integer)
    '***** Set selected item variable for drpTest2
    giIndex = index
    '***** Tell Excel to redraw ribbon
    '(you could invalidate only parts of the ribbon with InvalidateControl
    'or InvalidateControlMso)
    myRibbon.Invalidate
End Sub

'Callback for drpTest2 getSelectedItemIndex
Sub drpTest2GetSelectedItem(control As IRibbonControl, ByRef returnedVal)
    '***** Return selected item for drpTest2 based on value stored in giIndex
    returnedVal = giIndex
End Sub