excel vba 里 togglebutton 如何设置多个按钮按下一个其他全部弹起?
我有一组按钮,每个按钮对应一个工作表,选中的时候展示该工作表,并且其他工作表对应的ribbon按钮全部弹起,下面这些回调可以展示选中的工作表,但是不能实现其他按钮弹起,请...
我有一组按钮,每个按钮对应一个工作表,选中的时候展示该工作表,并且其他工作表对应的ribbon按钮全部弹起,下面这些回调可以展示选中的工作表,但是不能实现其他按钮弹起,请教该怎么做?谢谢!
Dim moRibbon As IRibbonUI
Public CurrentReport As String
'Callback for customUI.onLoad
Sub rxcustomUI_onLoad(ribbon As IRibbonUI)
Set moRibbon = ribbon
End Sub
'Callback for Sales_label onAction
Sub button_onAction(control As IRibbonControl, pressed As Boolean)
Worksheets(control.ID).Activate
CurrentReport = control.ID
End Sub
'Callback for Sales_label getPressed
Sub button_getPressed(control As IRibbonControl, ByRef returnedVal)
returnedVal = CurrentReport = control.ID
End Sub
XML的部分如下
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="rxcustomUI_onLoad">
<ribbon>
<tabs>
<tab id="DailySalesReport" label="Daily Sales Report" insertBeforeMso="TabHome" keytip="LY">
<group id="web" label="WEB">
<separator id="leonsep7"/>
<toggleButton id="sales_web" label="sales" onAction="button_onAction" getPressed="button_getPressed"/>
<toggleButton id="gp_web" label="GP" onAction="button_onAction" getPressed="button_getPressed"/>
<toggleButton id="gm_web" label="GM%" onAction="button_onAction" getPressed="button_getPressed"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI> 展开
Dim moRibbon As IRibbonUI
Public CurrentReport As String
'Callback for customUI.onLoad
Sub rxcustomUI_onLoad(ribbon As IRibbonUI)
Set moRibbon = ribbon
End Sub
'Callback for Sales_label onAction
Sub button_onAction(control As IRibbonControl, pressed As Boolean)
Worksheets(control.ID).Activate
CurrentReport = control.ID
End Sub
'Callback for Sales_label getPressed
Sub button_getPressed(control As IRibbonControl, ByRef returnedVal)
returnedVal = CurrentReport = control.ID
End Sub
XML的部分如下
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="rxcustomUI_onLoad">
<ribbon>
<tabs>
<tab id="DailySalesReport" label="Daily Sales Report" insertBeforeMso="TabHome" keytip="LY">
<group id="web" label="WEB">
<separator id="leonsep7"/>
<toggleButton id="sales_web" label="sales" onAction="button_onAction" getPressed="button_getPressed"/>
<toggleButton id="gp_web" label="GP" onAction="button_onAction" getPressed="button_getPressed"/>
<toggleButton id="gm_web" label="GM%" onAction="button_onAction" getPressed="button_getPressed"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI> 展开
1个回答
展开全部
你好,
请参考以下代码(XML 部分不作变动):
Option Explicit
Dim moRibbon As IRibbonUI
Dim strCurrentReport As String
Sub rxcustomUI_onLoad(ByVal Ribbon As IRibbonUI)
Set moRibbon = Ribbon
strCurrentReport = ActiveSheet.Name
End Sub
Sub button_onAction(ByVal Control As IRibbonControl, Pressed As Boolean)
strCurrentReport = Control.Id
Worksheets(strCurrentReport).Activate
moRibbon.Invalidate
End Sub
Sub button_getPressed(ByVal Control As IRibbonControl, ByRef ReturnedVal)
ReturnedVal = (strCurrentReport = Control.Id)
End Sub
运行效果:
追问
请问 moRibbon.Invalidate 在这里是什么作用呢?是清除控件状态吗?
button_getPressed 再重新对所有控件判断,确定状态?
谢谢!
追答
IRibbonUI.Invalidate 方法可以使自定义功能区用户界面的所有控件的缓存值无效,可以理解为你说的清除控件状态。
对于切换按钮来说,getPressed 回调函数用于获取切换按钮是否被按下的一个值,且仅在 Ribbon 首次加载(Ribbon.onLoad)时被调用。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询