Hide command button if cell has a value advice

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,602
Office Version
  1. 2007
Platform
  1. Windows
I have various command buttons on my worksheet.
I wish to hide / show specific command buttons depending on whether a specific cell has a value or not.

Just to start me off i have the following BUT the command button is shown all the time.
Can you advise what ive done wrong please.

Basicallically wish the relevant button to toggle visability whether it cell in question is empty or has a value.
Should be simple enough but cant see this error here ?
Thansk


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("B4") = "" Then ' If there is a value in cell then hide button
AprilStartButton.Visible = True
End If

If Range("E4") = "" Then ' If there is a value in cell then hide button
MayButton.Visible = True
End If

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Ok so i have now got it working with the following BUT it needs to be altered.
Reason is when i manually go down the page clicking cells then stop i can see the code still running & selecting cells as you work expect.

So i need it to be written correctly so it will be shorter & run fast / smooth
The below carries on for another few more months

VBA Code:
If Range("B4") = "" Then
AprilStartButton.Visible = True
Else
AprilStartButton.Visible = False
End If

If Range("E4") = "" Then
MayButton.Visible = True
Else
MayButton.Visible = False
End If

If Range("B19") = "" Then
JuneButton.Visible = True
Else
JuneButton.Visible = False
End If

If Range("E19") = "" Then
JulyButton.Visible = True
Else
JulyButton.Visible = False
End If

If Range("B34") = "" Then
AugustButton.Visible = True
Else
AugustButton.Visible = False
End If
 
Upvote 0
Is that the entire _SelectionChange code for this sheet ?
 
Upvote 0
No,
This is it supplied.
Just to advise you that i changed it from SELECTION CHANGE to CHANGE which helps a bit but as you can see the code is still far to long.
Thanks


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Range("B4") = "" Then
AprilStartButton.Visible = True
Else
AprilStartButton.Visible = False
End If

If Range("E4") = "" Then
MayButton.Visible = True
Else
MayButton.Visible = False
End If

If Range("B19") = "" Then
JuneButton.Visible = True
Else
JuneButton.Visible = False
End If

If Range("E19") = "" Then
JulyButton.Visible = True
Else
JulyButton.Visible = False
End If

If Range("B34") = "" Then
AugustButton.Visible = True
Else
AugustButton.Visible = False
End If

If Range("E34") = "" Then
septemberButton.Visible = True
Else
septemberButton.Visible = False
End If

If Range("B49") = "" Then
OctoberButton.Visible = True
Else
OctoberButton.Visible = False
End If

If Range("E49") = "" Then
NovemberButton.Visible = True
Else
NovemberButton.Visible = False
End If

If Range("B64") = "" Then
DecemberButton.Visible = True
Else
DecemberButton.Visible = False
End If

If Range("E64") = "" Then
JanuaryButton.Visible = True
Else
JanuaryButton.Visible = False
End If

If Range("B79") = "" Then
FebruaryButton.Visible = True
Else
FebruaryButton.Visible = False
End If

If Range("E79") = "" Then
MarchButton.Visible = True
Else
MarchButton.Visible = False
End If

If Range("B94") = "" Then
AprilEndButton.Visible = True
Else
AprilEndButton.Visible = False
End If

End Sub
 
Upvote 0
as you can see the code is still far to long.

Not tested but see if this update to your code will do what you want

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng         As Range, cell As Range
    Dim i           As Long
    
    Set rng = Me.Range("E64,B79,E79,E4,B19,E19,B34,E34,B49,E49,B64,B4,B94")
    
    i = 1
    If Not Intersect(Target, rng) Is Nothing Then
        Me.AprilStartButton.Visible = rng.Areas(12).Value = ""
        For Each cell In rng.Cells
            Me.OLEObjects(MonthName(i, False) & "Button").Visible = cell.Value = ""
            i = i + 1
            If i > 12 Then Exit For Else If i = 4 Then i = 5
        Next
        Me.AprilEndButton.Visible = rng.Areas(13).Value = ""
    End If
    
End Sub

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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