保留打开工作簿上的复选框功能区状态

时间:2017-04-13 11:35:36

标签: excel vba ribbon

我在功能区中创建了一个标签“MYTAB”,其中有2个复选框可隐藏和显示列。 我需要在重新打开工作簿时保留复选框状态。

CustomUi代码:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="rxIRibbonUI_onLoad">
<ribbon>
    <tabs>
        <tab id="myTab" label="MYTAB">
            <group id="Group1" label="Hide Columns">
                <checkBox 
                    id="chkClass12"
                    label=" Class12"
                    getPressed="rxClass_getPressed"
                    onAction="rxClass_onAction"/>
                <checkBox 
                    id="chkClass34"
                    label="Class34"
                    getPressed="rxClass_getPressed"
                   onAction="rxClass_onAction"/>
            </group>
        </tab>
    </tabs>
</ribbon>

模块代码:

Public b_chkClass12 As Boolean
Public b_chkClass34 As Boolean
Public MyRibbon As IRibbonUI

Sub rxIRibbonUI_onLoad(ribbon As IRibbonUI)
    Set MyRibbon = ribbon
End Sub

Public Sub rxClass_getPressed(control As IRibbonControl, ByRef returnedVal)
    Select Case control.ID
        Case "b_chkClass12":
            returnedVal = b_chkClass12

        Case "b_chkClass34":
            returnedVal = b_chkClass34
    End Select
End Sub

Public Sub rxClass_onAction(control As IRibbonControl, pressed As Boolean)
    Select Case control.ID
        Case "chkClass12":
            b_chkClass12 = pressed
            Worksheets("Sheet1").[oClass12].Value = pressed
            Call ShowHideColumns("Class12")

        Case "chkClass34":
            b_chkClass34 = pressed
            Worksheets("Sheet1").[oClass34].Value = pressed
            Call ShowHideColumns("Class34")
    End Select
End Sub

Sub ShowHideColumns(nRange As String)
    Range(nRange).EntireColumn.Hidden = Not Range(nRange).EntireColumn.Hidden
End Sub

此工作簿代码:

Private Sub Workbook_Open()
    With Worksheets("Sheet1").[oClass12]
        If .Value = True Then
            b_chkClass12 = True
        Else
            b_chkClass12 = False
        End If
    End With

    With Worksheets("Sheet1").[oClass34]
        If .Value = True Then
            b_chkClass34 = True
        Else
            b_chkClass34 = False
        End If
    End With
End Sub

保留值的工作表

enter image description here

名称管理员'

enter image description here

此致 埃利奥·费尔南德斯

0 个答案:

没有答案