Hiding and Unhiding buttons question

Alex Piotto

Board Regular
Joined
Jul 5, 2016
Messages
82
Office Version
  1. 2007
Platform
  1. Windows
Hi!
I have got two command buttons overlapping.
In the script below, CommandButton1 will hide the zero's rows, and CommandButton2 will show them again.
And they will hide and show each other too.
The hiding zeros part is OK.
Quite simple I can say... BUT....

VBA Code:
'HIDING ZERO ROWS
ActiveSheet.Shapes("CommandButton2").Visible = True
ActiveSheet.Shapes("CommandButton1").Visible = False

    Dim cell As Range
    For Each cell In Range("K2:K200")
    If Not IsEmpty(cell) Then
    If cell.Value <> "" And cell.Value = 0 Then
    cell.EntireRow.Hidden = True
    End If
    End If
    Next

VBA Code:
'SHOW ALL ROWS
ActiveSheet.Shapes("CommandButton1").Visible = True
ActiveSheet.Shapes("CommandButton2").Visible = False

    Dim cell As Range
    For Each cell In Range("K2:K200")
    cell.EntireRow.Hidden = False
    Next

... BUT... the buttons will hide only if I click somewhere on the sheet.
Why is that happening and what can I do to avoid it?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Can you please supply the entire code?
 
Upvote 0
Yes I can. Thanks. Actually what before seems to work is not. Buttons do not hide really... I am getting crazy over it!

VBA Code:
Private Sub CommandButton2_Click()
Application.ScreenUpdating = False


ThisWorkbook.Sheets("CONTAGGIO").Unprotect
'SHOW ALL
ActiveSheet.Shapes("CommandButton1").Visible = True
ActiveSheet.Shapes("CommandButton2").Visible = False

    Dim cell As Range
    For Each cell In Range("K2:K200")
    cell.EntireRow.Hidden = False
    Next
    


ThisWorkbook.Sheets("CONTAGGIO").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ThisWorkbook.Sheets("CONTAGGIO").EnableSelection = xlUnlockedCells

Application.ScreenUpdating = True
End Sub

VBA Code:
Private Sub CommandButton1_Click()


Application.ScreenUpdating = False

ThisWorkbook.Sheets("CONTAGGIO").Unprotect
'HIDING ZEROS
ActiveSheet.Shapes("CommandButton2").Visible = True
ActiveSheet.Shapes("CommandButton1").Visible = False

    Dim cell As Range
    For Each cell In Range("K2:K200")
    If Not IsEmpty(cell) Then
    If cell.Value <> "" And cell.Value = 0 Then
    cell.EntireRow.Hidden = True
    End If
    End If
    Next


ThisWorkbook.Sheets("CONTAGGIO").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ThisWorkbook.Sheets("CONTAGGIO").EnableSelection = xlUnlockedCells

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for that, both those codes work for me.
Do you get any error messages?
Are the buttons on the Contaggio sheet?
 
Upvote 0
No errors.... and the buttons are in contaggio sheet.
May be is all about the initial visibility of the buttons?
Any alternative?
 
Upvote 0
Does it make any difference if you use
VBA Code:
Me.CommandButton1.Visible = True
Me.CommandButton2.Visible = False
 
Upvote 0
Have you changed the name(s) of your command buttons in the Properties window next to (Name)?
 
Upvote 0
Is the property name of your command button "CommandButton1" and "CommandButton2"?

Also is that region of your worksheet unlocked or your worksheet unprotected?
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,824
Members
449,190
Latest member
rscraig11

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