我正在使用自定义 UI 编辑器为 Excel 制作一个自定义选项卡,其中有两个 DropDown 控件。我们称它们为 DropDown1 和 DropDown2。我的目标是,每当我更改 DropDown1 选择时,它都会自动更改 de DropDown2 选择,但我不知道如何在 DropDown 控件中设置“SelectedItem”。
到目前为止,我有一个 VBA 函数,每次更改 DropDown1 的选择时都会触发该函数,我认为这会有所帮助。
您需要在自定义 UI 编辑器中向您的功能区 XML 添加一个回调函数,然后将相应的代码添加到您的 VBA 项目中,以便在功能区选项卡失效时调用。您需要为下拉控件设置所选项目的回调是getSelectedItemIndex
或getSelectedItemID
,具体取决于您是要按索引还是按 id 选择项目。由于您没有提供任何代码,因此我的示例是通用的(未经测试):
功能区 XML:
<dropDown id="drpTest" label="Test dropdown" getSelectedItemIndex="drpTestGetSelectedItem" ></dropDown>
VBA 回调
'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
编辑:
基于其他下拉列表选择索引的示例。在类似的解决方案中,我在onAction
一个控件的函数中设置了一个值,并使用它在另一个控件中设置选定的索引,如下所示:
功能区 XML:
<dropDown id="drpTest1" label="Test dropdown 1" onAction="drpTest1OnAction" ></dropDown>
<dropDown id="drpTest2" label="Test dropdown 2" getSelectedItemIndex="drpTest2GetSelectedItem" ></dropDown>
VBA 回调
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