我试图找出如何根据Selection启用功能区按钮,我知道我需要使用Worksheet_SelectionChange事件,但我不知道如何继续。我已经筋疲力尽所有关于如何做到这一点的选择,请有人帮忙解决这个问题,我已经问过Excel先生,但没有找到我需要的答案。 我正在寻找的例子是:
如果选择了列,则启用功能区按钮a或如果选择了行,则启用功能区按钮b
答案 0 :(得分:0)
使用功能区回调并在适当的位置拨打IRibbonUI.Invalidate或IRibbonUI.InvalidateControl。有关详细信息,请参阅How to get the reference to the IRibbonUI in VBA?。
您可以使用VBA宏或COM加载项中的回调过程自定义功能区UI。对于加载项实现的每个回调,响应都被缓存。例如,如果加载项编写器为按钮实现getImage
回调过程,则调用该函数一次,加载图像,然后如果需要更新图像,则使用缓存的图像而不是调用程序,流程。此过程保持原位,直到代码通过使用Invalidate
方法发出缓存值无效的信号,此时,再次调用回调过程并缓存返回响应。然后,加载项或VBA宏可以通过调用Refresh
方法强制立即更新UI。
在自定义UI XML文件中,您需要声明onLoad
回调:
<customUI … onLoad=”MyAddInInitialize” …>
然后在VBA中你可以使用:
Dim MyRibbon As IRibbonUI
Sub MyAddInInitialize(Ribbon As IRibbonUI)
Set MyRibbon = Ribbon
End Sub
Sub myFunction()
‘ Invalidates the caches of all of this add-in’s controls
MyRibbon.Invalidate()
End Sub
在以下文章中阅读有关Fluent UI(又名Ribbon UI)的更多信息:
答案 1 :(得分:0)
这是我到目前为止所做的,但我仍然是新手。
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnRibbonLoad">
<ribbon>
<tabs>
<tab id="MyTools" label="Tools">
<group id="MoveGroup" label="Move" tag="GroupMove" >
<button id="MoveColumnLeft" tag="EnableLeft" imageMso="GoRtl" screentip="Move Column Left" supertip="Move the selected column to the left" label="Left" size="large" onAction="OnActionButton" getEnabled="GetEnabled" />
<button id="MoveColumnRight" tag="EnableRight" imageMso="GoLeftToRight" screentip="Move Column Right" supertip="Move the selected column to the right" label="Right" size="large" onAction="OnActionButton" getEnabled="GetEnabled" />
`<separator id="MoveSep" />`
<button id="MoveRowUp" tag="EnableUp" imageMso="MessagePrevious" screentip="Move Row Up" supertip="Move the selected row up" label="Up" size="large" onAction="OnActionButton" getEnabled="GetEnabled" />
<button id="MoveRowDown" tag="EnableDown" imageMso="MessageNext" screentip="Move Row Down" supertip="Move the selected row down" label="Down" size="large" onAction="OnActionButton" getEnabled="GetEnabled" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Option Explicit
Public oRibbon As IRibbonUI, bEnabled As Boolean
Sub OnRibbonLoad(ribbon As IRibbonUI)
Set oRibbon = ribbon
bEnabled = True
End Sub
Sub OnActionButton(control As IRibbonControl)
Select Case control.ID
Case "MoveColumnLeft"
bEnabled = enabled
oRibbon.Invalidate
Case "MoveColumnRight"
bEnabled = enabled
oRibbon.Invalidate
End Select
End Sub
Sub GetEnabled(control As IRibbonControl, ByRef enabled)
`Select Case control.ID
Case "MoveColumnLeft"
enabled = enabled
Case "MoveColumnRight"
enabled = enabled
End Select
End Sub