Hiding Command Buttons

Heyjoe

New member
Joined
Jan 3, 2019
Messages
59
Reaction score
0
Points
0
Location
USA
Excel Version(s)
2019
VBA programmers,

I have a worksheet with 8 command buttons on it. I wrote the following code to hide the last 4 command buttons.

Code:
Public Sub Practice()
Sheets("prac").Select
Dim n As Byte
For n = 6 To 8
ActiveSheet.Shapes.Range(Array(n)).Visible = True
Next n

End Sub

It made the 3, 5 & 6 command buttons invisible.

Is there a better way to do this or do I have to specify which command buttons that I want to make invisible by the name of the command button?
 
The bet is to name the buttons. Name the buttons "Button 1" ... "Button 8".

This code will toggle the visibility

Code:
Public Sub Practice()
  
  Dim sh As Shape
  
  For Each sh In ActiveSheet.Shapes
    Debug.Print sh.Name
    If CInt(Right(sh.Name, 1)) >= 5 Then
      sh.Visible = Not sh.Visible
    End If
  Next

End Sub
 
Thanks Gue,

It works very well. I think the line
Code:
sh.Visible = Not sh.Visible
is a very smart way to do toggles. In my code I always checked to see if a toggle was on or off using an IF statement, then if it was on I turned it off (or vice versa). This will make my code more efficient.
 
Heyjoe, thanks for your feedback
 
Back
Top