如何在初始化期间隐藏某些用户表单字段?

时间:2019-03-20 15:41:06

标签: excel vba userform

因此,我有一个由Multipages组成的用户窗体,其中一个页面包含togglebuttons,这些页面可隐藏和取消隐藏用户窗体以及excel工作表上的字段。下图显示了切换按钮页面。

enter image description here

HAZOP / SIL和LOPA的代码相同,只是它隐藏的字段不同。下面是HAZOP切换按钮的代码。

Private Sub togbHAZOP_Click()

    If togbHAZOP = True Then

        Sheets("Updated Hours EST").Rows("6:27").EntireRow.Hidden = False
        Sheets("SCOPE").Rows("31:37").EntireRow.Hidden = False
        Sheets("SUMMARY").Rows("5:8").EntireRow.Hidden = False
        Frame5.Enabled = True
        Frame5.Visible = True
        Frame6.Enabled = True
        Frame6.Visible = True
        Frame7.Enabled = True
        Frame7.Visible = True
        HazOp.Enabled = True
        HazOp.Visible = True

    Else
        Sheets("Updated Hours EST").Rows("6:27").EntireRow.Hidden = True
        Sheets("SCOPE").Rows("31:37").EntireRow.Hidden = True
        Sheets("SUMMARY").Rows("5:8").EntireRow.Hidden = True
        Frame5.Enabled = False
        Frame5.Visible = False
        Frame6.Enabled = False
        Frame6.Visible = False
        Frame7.Enabled = False
        Frame7.Visible = False
        HazOp.Enabled = False
        HazOp.Visible = False

    End If

End Sub

目前用于初始化的代码,但是它不起作用,并显示错误消息"Run-time error 438: Object doesn't support this property or method"

Private Sub UserForm_Initialize()

    WizardProp.MultiPage1.Value = 0
    Me.MultiPage1.Style = fmTabStyleNone
    togbHAZOP.Frame5.Enabled = False
    togbHAZOP.Frame5.Visible = False
    togbHAZOP.Frame6.Enabled = False
    togbHAZOP.Frame6.Visible = False
    togbHAZOP.Frame7.Enabled = False
    togbHAZOP.Frame7.Visible = False
    togbHAZOP.HazOp.Enabled = False
    togbHAZOP.HazOp.Visible = False

End Sub

我遇到的问题是如何在代码开头隐藏用户表单中的某些字段,并且根据用户使用togglebuttons所做的选择将隐藏/取消隐藏,因为在我必须先单击切换按钮,然后再单击以使其隐藏链接字段,此刻每次在初始化过程中都会造成麻烦。

1 个答案:

答案 0 :(得分:2)

请注意,您可以减少如下代码。在True部分将togbHAZOP.Value替换为False,将Not togbHAZOP.Value替换为If togbHAZOP = True。因此,您无需重复整个代码。

Private Sub togbHAZOP_Click()
    Sheets("Updated Hours EST").Rows("6:27").EntireRow.Hidden = Not togbHAZOP.Value
    Sheets("SCOPE").Rows("31:37").EntireRow.Hidden = Not togbHAZOP.Value
    Sheets("SUMMARY").Rows("5:8").EntireRow.Hidden = Not togbHAZOP.Value
    Frame5.Enabled = togbHAZOP.Value
    Frame5.Visible = togbHAZOP.Value
    Frame6.Enabled = togbHAZOP.Value
    Frame6.Visible = togbHAZOP.Value
    Frame7.Enabled = togbHAZOP.Value
    Frame7.Visible = togbHAZOP.Value
    HazOp.Enabled = togbHAZOP.Value
    HazOp.Visible = togbHAZOP.Value    
End Sub

由于togbHAZOP是切换按钮,因此没有框架togbHAZOP.Frame5可能是这里的问题。可能应该是这样的:

Private Sub UserForm_Initialize()
    WizardProp.MultiPage1.Value = 0
    Me.MultiPage1.Style = fmTabStyleNone
    Me.Frame5.Enabled = False
    Me.Frame5.Visible = False
    Me.Frame6.Enabled = False
    Me.Frame6.Visible = False
    Me.Frame7.Enabled = False
    Me.Frame7.Visible = False
    Me.HazOp.Enabled = False
    Me.HazOp.Visible = False
End Sub