这是此问题的后续问题:
Excel Ribbon 组合框:始终显示当前选择并在保存前记住选择
我正在尝试设置一个组合框来选择工作表。
“原始”代码有两个问题:
FaneDuru 解释了完成这项工作所需的所有必要步骤和代码片段。
不幸的是,我一定错过了一些东西。
第二个问题已解决,工作簿现在打开并显示最后的选择。第一个问题仍然存在:当单击工作表的选项卡时,组合框尚未刷新。我想我将用于写入/读取注册表的变量放置在错误的位置?它们应该位于单独的模块中吗?
RibUI.InvalidateControl "ComboBox001"
有两个地方,对吗?在“ThisWorkbook”以及回调模块中?
谢谢!
--XML
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="LoadRibbon">
<ribbon>
<tabs>
<tab id="Tabv3.1" label="TOOLS" insertAfterMso="TabHome">
<group id="GroupDemo2"
label="SelectSheet"
imageMso="AddInManager">
<comboBox id="ComboBox001"
label="comboBox001"
getText="ComboBox001_GetText"
onChange="ComboBox001_OnChange">
<item id="ItemOne"
label="One"/>
<item id="ItemTwo"
label="Two"/>
<item id="ItemThree"
label="Three"/>
</comboBox>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
--Callback VBA in Module "RibbonCallbacks"
Option Explicit
Public RibUI As IRibbonUI
Public Const myApp As String = "RibbApp", mySett As String = "Settings", myVal As String = "Value"
Sub LoadRibbon(Ribbon As IRibbonUI)
Set RibUI = Ribbon
RibUI.InvalidateControl "ComboBox001"
End Sub
'Callback for ComboBox001 onChange
Sub ComboBox001_OnChange(control As IRibbonControl, id As String)
Select Case id
Case "One"
ThisWorkbook.Sheets("Sheet1").Select
Case "Two"
ThisWorkbook.Sheets("Sheet2").Select
Case "Three"
ThisWorkbook.Sheets("Sheet3").Select
End Select
RibUI.InvalidateControl "ComboBox001"
SaveSetting myApp, mySett, myVal, id
End Sub
'Callback for ComboBox001 getText
Sub ComboBox001_getText(control As IRibbonControl, ByRef returnedVal)
Dim comboVal As String
comboVal = GetSetting(myApp, mySett, myVal, "No Value") 'read it from Registry
If comboVal <> "No Value" Then
returnedVal = comboVal
End If
End Sub
--VBA in "ThisWorkbook"
Private Sub Workbook_Open()
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
RibUI.InvalidateControl "ComboBox001"
End Sub
SaveSetting
事件中的Workbook_SheetActivate
。Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim idx As String
Select Case Sh.Name
Case "Sheet1"
idx = "One"
Case "Sheet2"
idx = "Two"
Case "Sheet3"
idx = "Three"
End Select
SaveSetting myApp, mySett, myVal, idx
RibUI.InvalidateControl "ComboBox001"
End Sub