+ Reply to Thread
Results 1 to 10 of 10

Hide a button

Hybrid View

  1. #1
    Registered User
    Join Date
    05-09-2007
    Posts
    28

    Question Hide a button

    Hi all,

    Hopefully this is a simple one, what would be the vba code to hide a button according to the value of a cell?

    Thanks in advance,

    Lee

  2. #2
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    Here is a way:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Address = "$A$1" And Target.Value = 1 Then
    Sheets(1).CommandButton1.Visible = False
    
    Else
    Sheets(1).CommandButton1.Visible = True
    
    End If
    
    
    End Sub

  3. #3
    Registered User
    Join Date
    05-09-2007
    Posts
    28
    Thanks, appreciate that.

    Is there a way of writing it instead of the Private Sub so that I can assign it to a button in Excel?

    Cheers,

    Lee

  4. #4
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    yes..

    Sub showCMD()
    If Sheets(1).Range("A1").value = 1 Then
    Sheets(1).CommandButton1.Visible = False
    
    Else
    Sheets(1).CommandButton1.Visible = True
    
    End If
    End Sub

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Lee & Steve,

    Here is way to write the macro so the button name isn't hard code into the macro. This way the macro can be assigned to a Button without knowing its name. This works only with Forms buttons, not Control Toolbox command buttons.

    Sub HideButton()
    
      With ActiveSheet.Buttons(Application.Caller)
        If Range("A1").Value = 1 Then
           .Visible = False
        Else
           .Visble = True
        End If
      End With
    
    End Sub
    Sincerely,
    Leith Ross

  6. #6
    Registered User
    Join Date
    05-09-2007
    Posts
    28
    Thank you very much!

    One silly questions, if the button was called "Button 42" how is that effected?

    I changed "CommandButton1" to "Button42" and "CommandButton42" but it didn't work.. any idea?

    Cheers once more,

    Lee

  7. #7
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    have a look at the properties of the bottom and be sure you are referring to by its (name)....Not its caption.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1