Reputation: 13
I have a user form with a single button.
Question: Is it possible to show or hide a worksheet using only a single button? If so what the code will look like?
I tried this one so far:
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
Upvotes: 1
Views: 3916
Reputation: 12602
Try this:
Private Sub data_Button_Click()
With Worksheets("u")
.Visible = Not .Visible
data_Button.Caption = IIf(.Visible, "Hide", "Show")
End With
End Sub
Upvotes: 2
Reputation: 24356
Merge those 2 If
statements into one like this:
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
Upvotes: 0