Hide button on worksheet if there is nothing contained in cell E24

TheJay

Active Member
Joined
Nov 12, 2014
Messages
364
Office Version
  1. 2019
Platform
  1. Windows
I've written some code but it's not working and I am not sure why.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    If Cells(5, 24).Value <> "1" Then
        ActiveSheet.Shapes("Button412").Visible = True
        Else
        ActiveSheet.Shapes("Button412").Visible = False
    End If
    Application.ScreenUpdating = True
End Sub

I want to make sure the button is hidden when the sheet is opened and only when a value appears in E24 should the button appear.

Could someone please point out my mistake and how to fix it?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Change the button name to suit your needs.
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    If Cells(5, 24).Value <> "1" Then
        ActiveSheet.Shapes.Range(Array("Button 412")).Visible = True
    Else
        ActiveSheet.Shapes.Range(Array("Button 412")).Visible = False
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for your reply. I've added it but it's not yet working as expected.

In E24, there is a formula

VBA Code:
=IFERROR(IF(ISBLANK(D4),"",(SUM(E4:E23)))," ")

Perhaps I need to refine the code? It's looking for an amount of time.
 
Upvote 0
If you are saying that you want to hide/unhide the button depending on the value in E24 and the value of E24 is the result of a formula, then try:
VBA Code:
Private Sub Worksheet_Calculate()
    If Range("E24") <> 1 Then
        ActiveSheet.Shapes.Range(Array("Button 412")).Visible = True
    Else
        ActiveSheet.Shapes.Range(Array("Button 412")).Visible = False
    End If
End Sub
 
Upvote 0
Thanks for your reply.

I've updated the button name and adapted the code but it's not hiding the button.

VBA Code:
Private Sub Worksheet_Activate()
    Range("B4").Select
    With Worksheets("Tracker")
    With ActiveWindow
        .DisplayFormulas = False
        .DisplayHeadings = False
        .DisplayGridlines = False
        .DisplayHorizontalScrollBar = False
        .DisplayVerticalScrollBar = False
    End With
    With Application
        .DisplayFullScreen = True
        .DisplayFormulaBar = False
        .DisplayStatusBar = False
    End With
    With Application
        .CommandBars("Full Screen").Visible = True
        .CommandBars("Worksheet Menu Bar").Enabled = False
        .CommandBars("Standard").Visible = False
        .CommandBars("Formatting").Visible = False
    End With
End With
End Sub

Private Sub Worksheet_Calculate()
    If Range("E24") <> 1 Then
        ActiveSheet.Shapes.Range(Array("Save")).Visible = True
    Else
        ActiveSheet.Shapes.Range(Array("Save")).Visible = False
    End If
End Sub

Before updating the button name it didn't work either.

I've not seen Worksheet_Calculate before. Will it initialise when the worksheet is opened? Also, should this code be able to cope with a time? It's a cumulative figure, so it would appear as 7:24 for example.
 
Upvote 0
Worksheet_Calculate triggers the macro whenever a formula in the sheet is calculated. If you want it to trigger when the sheet is opened, then place the code in the Worksheet_Activate event. What kind of button are you using? Is it a Form Control button, ActiveX button or a simple shape?
 
Upvote 0
Thanks for your reply. It's a form control button. So if I want this to work correctly, I need to insert in activate AND include calculate, because it's expected the user will input data after opening the sheet. The formula I included before doesn't produce a value until the user enters data into three cells.
 
Upvote 0
Since the hide/unhide depends on the value of E24 which is the result of a formula, you need to use the Worksheet_Calculate event. You don't need the Worksheet_Activate event unless activating the sheet causes the value of E24 to change. If it's still not working, perhaps you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here (de-sensitized if necessary).
 
Upvote 0
Sure, we'll try that next. From your questioning, you got me wondering whether "Shapes" was the right thing to reference.

VBA Code:
Private Sub Worksheet_Calculate()
    If Range("E24") <> 1 Then
        ActiveSheet.Buttons("Save").Visible = True
    Else
        ActiveSheet.Buttons("Save").Visible = False
    End If
End Sub

I tried this but it's still not working. Have I done this right?
 
Upvote 0
Yes. If I could see your file, it would help.
 
Upvote 0

Forum statistics

Threads
1,214,608
Messages
6,120,500
Members
448,968
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top