2017-04-23 134 views
0

我正在使用自定義用戶界面編輯器爲Excel創建自定義選項卡,並且其中有兩個DropDown控件。我們稱它們爲DropDown1和DropDown2。我的目標是每當我更改DropDown1選擇時,它會自動更改de DropDown2選擇,但我不知道如何在DropDown控件中設置「SelectedItem」。如何在自定義DropDown功能區控件上設置選定的項目

到目前爲止,我有一個VBA函數,每當我更改DropDown1的選擇時觸發,我認爲這可以是有幫助的。

+0

這些數據驗證下拉列表或activeX組合框?你應該發佈一些代碼,以免混淆。 – Amorpheuses

+0

@Amorpheuses這是在Excel的功能區中添加的DropDown控件。這是對象:https://msdn.microsoft.com/en-us/library/dd947478(v=office.12).aspx –

回答

0

您需要在自定義用戶界面編輯器中向功能區XML添加回調函數,然後將相應的代碼添加到您要在功能區選項卡失效時調用的VBA項目中。您需要爲下拉控件設置所選項目的回調爲getSelectedItemIndexgetSelectedItemID,具體取決於您是否要按索引或ID選擇項目。既然你沒有提供任何代碼,我examle是一般的(而不是測試):

區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 
+0

謝謝Olle!但我仍然沒有得到「如何」。所以我有2 DropDown,(據我所知)我會有2「getSelectedItemIndex」函數。這些函數在其DropDown選項更改時將被調用。因此,在我的「DropDown1」的「getSelectedItemIndex」函數中,需要一些代碼來設置「DropDown2」的SelectedItem。我仍然不知道該怎麼做。我會很感激這方面的幫助。 –

+0

當功能區失效並需要重繪時調用'getSelectedItemIndex',因此您可以使用它來決定選擇哪個索引。你如何決定哪個指數取決於你。將添加一個例子給我的答案。 –

相關問題