命令后用户窗体按钮保持按下状态

时间:2019-05-06 08:31:24

标签: excel vba

我有一个带有与代码连接的按钮的用户窗体。为什么在代码完成后仍不断按下按钮?有什么办法释放它们?

单击按钮后,其外观如下:

enter image description here

按钮代码:

Private Sub ToggleButton2_Click()
ThisWorkbook.Worksheets("Price calculation").Activate

Application.ScreenUpdating = False
ThisWorkbook.Sheets("Price calculation").Unprotect Password:="123"
Range("1867:1979").EntireRow.Hidden = False
ActiveSheet.Shapes("Rectangle: Rounded Corners 76").Visible = False
ActiveSheet.Shapes("Rectangle: Rounded Corners 244").Visible = True

ActiveWindow.ScrollRow = 1867
ThisWorkbook.Sheets("Price calculation").Protect Password:="123"
Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:2)

您正在使用切换按钮。这些毫不奇怪地切换。我认为您正在寻找改用CommandButton。

在工具箱中浏览以找到CommandButton控件,将ToggleButton替换为新添加的CommandButton。然后,编辑您的切换按钮的代码,并将其替换为:

Private Sub CommandButton1_Click()

Application.ScreenUpdating = False
With ThisWorkbook.Sheets("Price calculation")
    .Activate     
    .Unprotect Password:="123"
    .Range("1867:1979").EntireRow.Hidden = False
    .Shapes("Rectangle: Rounded Corners 76").Visible = False
    .Shapes("Rectangle: Rounded Corners 244").Visible = True

    ActiveWindow.ScrollRow = 1867 'Is this necessary? It scrolls to a specific hard-coded row
    .Protect Password:="123"
End With
Application.ScreenUpdating = True
End Sub