我有一个带有一个按钮的用户表单。
问题:是否可以仅使用一个按钮显示或隐藏工作表?如果是这样,代码会是什么样的?
到目前为止我试过这个:
Private Sub data_Button_click()
If data_Button.Caption = "Hidden" Then
Worksheets("u").Visible = True
data_Button.Caption = "Visible"
End If
If data_Button.Caption = "Visible" Then
Worksheets("u").Visible = False
data_Button.Caption = "Hidden"
End If
End Sub
答案 0 :(得分:2)
试试这个:
Private Sub data_Button_Click()
With Worksheets("u")
.Visible = Not .Visible
data_Button.Caption = IIf(.Visible, "Hide", "Show")
End With
End Sub
答案 1 :(得分:0)
将这两个If
语句合并为一个这样的语句:
Private Sub data_Button_click()
If data_Button.Caption = "Hidden" Then
Worksheets("u").Visible = True
data_Button.Caption = "Visible"
ElseIf data_Button.Caption = "Visible" Then
Worksheets("u").Visible = False
data_Button.Caption = "Hidden"
End If
End Sub