group id="MoveGroups" label="Move"
toggleButton id="MoveCR" size="large" imageMso="DrawingCanvasExpand" getLabel="getToggleLabel" getPressed="getTogglePressed" onAction="onToggleAction" screentip = "This hides or shows Move Buttons"
group
group id="MoveColumns" tag="GroupMove" getVisible = "getGroupVisible" label="Column"
button id="MoveColumnLeft" tag="EnableLeft" imageMso="TableColumnsInsertLeft" screentip="Move Column Left" supertip="Move the selected column to the left" label="Left" size="large" onAction="MoveColumnLeft"
button id="MoveColumnRight" tag="EnableRight" imageMso="TableColumnsInsertRight" screentip="Move Column Right" supertip="Move the selected column to the right" label="Right" size="large" onAction="MoveColumnRight"
group
group id="MoveRows" tag="GroupMove" getVisible = "getGroupVisible" label="Row"
button id="MoveRowUp" tag="EnableUp" imageMso="TableRowsInsertAboveWord" screentip="Move Row Up" supertip="Move the selected row up" label="Up" size="large" onAction="MoveRowUp"
button id="MoveRowDown" tag="EnableDown" imageMso="TableRowsInsertBelowWord" screentip="Move Row Down" supertip="Move the selected row down" label="Down" size="large" onAction="MoveRowDown"
group
Option Explicit
Public bPressedMoveCR As Boolean, bVisibleMoveCR As Boolean
Sub OnRibb******(ribbon As IRibbonUI)
'
Set oRibbon = ribbon
bPressedMoveCR = False
End Sub
Sub getToggleLabel(control As IRibbonControl, ByRef returnedVal)
'
Select Case control.ID
Case "MoveCR"
If bVisibleMoveCR Then
returnedVal = "Hide Move"
Else
returnedVal = "Show Move"
End If
End Select
End Sub
Sub getTogglePressed(control As IRibbonControl, ByRef returnedVal)
'
Select Case control.ID
Case "MoveCR"
returnedVal = bPressedMoveCR
End Select
End Sub
Sub onToggleAction(control As IRibbonControl, pressed As Boolean)
'
Select Case control.ID
Case "MoveCR"
bPressedMoveCR = Not bPressedMoveCR
bVisibleMoveCR = Not bVisibleMoveCR
oRibbon.Invalidate
End Select
End Sub
Sub getGroupVisible(control As IRibbonControl, ByRef returnedVal)
'
Select Case control.ID
Case "MoveColumns"
returnedVal = bVisibleMoveCR
Case "MoveRows"
returnedVal = bVisibleMoveCR
End Select
End Sub
<button id="MoveColumnRight"
tag="EnableRight"
imageMso="TableColumnsInsertRight"
screentip="Move Column Right"
supertip="Move the selected column to the right"
label="Right"
getEnabled="GetEnabledMacro"
size="large"
onAction="MoveColumnRight"/>
Private Sub Workbook_Open()
Call UpdateMoveRibbonButtons
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Call UpdateMoveRibbonButtons
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Call UpdateMoveRibbonButtons
End Sub
Dim oRibbon As IRibbonUI
Dim msDirection As String
Sub UpdateMoveRibbonButtons()
'--if entire row(s) or column(s) are selected, then
' enable corresponding ribbon buttons
'--buttons will only be enabled if user selects 1 entire
' row or column. Modify these values to allow user to
' select more than one row or column
Const lMAX_COL = 1
Const lMAX_ROW = 1
With Selection
Select Case True
Case TypeName(Selection) <> "Range"
'--selected object is not range-disable move buttons
Call RefreshRibbon(sDirection:=vbNullString)
Case .Rows.Count = .Parent.Rows.Count And _
.Columns.Count <= lMAX_COL
'--entire column(s) selected
Call RefreshRibbon(sDirection:="Column")
Case .Columns.Count = .Parent.Columns.Count And _
.Rows.Count <= lMAX_ROW
'--entire row(s) selected
Call RefreshRibbon(sDirection:="Row")
Case Else
'--disable move buttons
Call RefreshRibbon(sDirection:=vbNullString)
End Select
End With
End Sub
Sub RefreshRibbon(sDirection As String)
' based on https://www.rondebruin.nl/win/s2/win013.htm
msDirection = sDirection
If oRibbon Is Nothing Then
MsgBox "Error, Save/Restart your workbook" & vbNewLine & _
"Visit this page for a solution:" & _
"https://www.mrexcel.com/forum/excel-questions/518629-how-preserve-regain-id-my-custom-ribbon-ui.html"
Else
oRibbon.Invalidate
End If
End Sub
Dim oRibbon As IRibbonUI
Dim msDirection As String
Public CEnabled As Boolean, REnabled As Boolean, bPressedMoveCR As Boolean, bVisibleMoveCR As Boolean
Sub OnRibb******(ribbon As IRibbonUI)
'
Set oRibbon = ribbon
bPressedMoveCR = False
CEnabled = True
REnabled = True
End Sub
Sub getToggleLabel(control As IRibbonControl, ByRef returnedVal)
'
Select Case control.ID
Case "MoveCR"
If bVisibleMoveCR Then
returnedVal = "Hide Move"
Else
returnedVal = "Show Move"
End If
End Select
End Sub
Sub getTogglePressed(control As IRibbonControl, ByRef returnedVal)
'
Select Case control.ID
Case "MoveCR"
returnedVal = bPressedMoveCR
End Select
End Sub
Sub onToggleAction(control As IRibbonControl, pressed As Boolean)
'
Case "MoveCR"
bPressedMoveCR = Not bPressedMoveCR
bVisibleMoveCR = Not bVisibleMoveCR
oRibbon.Invalidate
End Select
End Sub
Sub getGroupVisible(control As IRibbonControl, ByRef returnedVal)
'
Select Case control.ID
Case "MoveColumns"
returnedVal = bVisibleMoveCR
Case "MoveRows"
returnedVal = bVisibleMoveCR
End Select
End Sub
Sub GetEnabledMacro(control As IRibbonControl, ByRef enabled)
'
Select Case control.ID
Case "MoveColumnLeft"
enabled = CEnabled
Case "MoveColumnRight"
enabled = CEnabled
Case "MoveRowUp"
enabled = REnabled
Case "MoveRowDown"
enabled = REnabled
Case "MoveColumnLeft"
enabled = Not (CEnabled)
Case "MoveColumnRight"
enabled = Not (CEnabled)
Case "MoveRowUp"
enabled = Not (REnabled)
Case "MoveRowDown"
enabled = Not (REnabled)
End Select
End Sub
Sub UpdateMoveRibbonButtons()
'
Const lMAX_COL = 1
Const lMAX_ROW = 1
With Selection
Select Case True
Case TypeName(Selection) <> "Range"
Call RefreshRibbon(sDirection:=vbNullString) '--selected object is not range-disable move buttons
Case .Rows.Count = .Parent.Rows.Count And .Columns.Count <= lMAX_COL
Call RefreshRibbon(sDirection:="Column") '--entire column(s) selected
Case .Columns.Count = .Parent.Columns.Count And .Rows.Count <= lMAX_ROW
Call RefreshRibbon(sDirection:="Row") '--entire row(s) selected
Case Else
Call RefreshRibbon(sDirection:=vbNullString) '--disable move buttons
End Select
End With
End Sub
Sub RefreshRibbon(sDirection As String)
'
msDirection = sDirection
If oRibbon Is Nothing Then
MsgBox "Error, Save/Restart your workbook"
Else
oRibbon.Invalidate
End If
End Sub
<group id="MoveGroups" label="Move">
<toggleButton id="MoveCR"
size="large"
imageMso="DrawingCanvasExpand"
getLabel="getToggleLabel"
getPressed="getTogglePressed"
onAction="onToggleAction" screentip = "This hides or shows Move Buttons" />
</group>
<group id="MoveColumns" tag="GroupMove" getVisible = "getGroupVisible" label="Column">
<button id="MoveColumnLeft"
tag="EnableLeft"
getEnabled="GetEnabledMacro"
imageMso="TableColumnsInsertLeft"
screentip="Move Column Left"
supertip="Move the selected column to the left"
label="Left" size="large"
onAction="MoveColumnLeft" />
<button id="MoveColumnRight"
tag="EnableRight"
getEnabled="GetEnabledMacro"
imageMso="TableColumnsInsertRight"
screentip="Move Column Right" supertip="Move the selected column to the right"
label="Right"
size="large"
onAction="MoveColumnRight" />
</group>
<group id="MoveRows" tag="GroupMove" getVisible = "getGroupVisible" label="Row">
<button id="MoveRowUp"
tag="EnableUp"
getEnabled="GetEnabledMacro"
imageMso="TableRowsInsertAboveWord"
screentip="Move Row Up" supertip="Move the selected row up" label="Up"
size="large"
onAction="MoveRowUp" />
<button id="MoveRowDown"
tag="EnableDown"
getEnabled="GetEnabledMacro"
imageMso="TableRowsInsertBelowWord"
screentip="Move Row Down" supertip="Move the selected row down"
label="Down"
size="large"
onAction="MoveRowDown" />
</group>