Results 1 to 4 of 4

Thread: Hiding Command Buttons

  1. #1
    Acolyte Heyjoe's Avatar
    Join Date
    Jan 2019
    Location
    USA
    Posts
    52
    Articles
    0
    Excel Version
    2019

    Hiding Command Buttons



    Register for a FREE account, and/
    or Log in to avoid these ads!

    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?

  2. #2
    Seeker gue's Avatar
    Join Date
    Nov 2018
    Posts
    19
    Articles
    0
    Excel Version
    Office Professional Plus 2016
    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

  3. #3
    Acolyte Heyjoe's Avatar
    Join Date
    Jan 2019
    Location
    USA
    Posts
    52
    Articles
    0
    Excel Version
    2019
    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.

  4. #4
    Seeker gue's Avatar
    Join Date
    Nov 2018
    Posts
    19
    Articles
    0
    Excel Version
    Office Professional Plus 2016
    Heyjoe, thanks for your feedback

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •