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?
 
Not sure what you mean?
Did you try what I suggested?

Hi Fluff, if I remember well the use of me.CommandButton1 is used inside a form. I have no forms in my book... and no, not tried yet. Later. Having Family dinner...
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi!.
Is the property name of your command button "CommandButton1" and "CommandButton2"?

Also is that region of your worksheet unlocked or your worksheet unprotected?
The buttons have the correct names. And the code unprotects the sheet before hiding the zeros and protects when finished.
 
Upvote 0
You would have to upload to a share site such as OneDrive, Google Drive, mark for sharing & then post the link to the thread.

If you are happy swapping two command buttons for a shape, you could assign this code to the shape & it will act as a toggle
VBA Code:
Sub AlexPiotto()
   Dim Shp As Shape
   Dim Cl As Range
  
   With Sheets("Contaggio")
      .Unprotect
      Set Shp = .Shapes(Application.Caller)
      If Shp.TextFrame.Characters.Caption = "Hide" Then
         For Each Cl In .Range("K2:K200")
            If Cl.Value <> "" And Cl.Value = 0 Then Cl.EntireRow.Hidden = True
         Next Cl
         Shp.TextFrame.Characters.Caption = "Unhide"
      Else
         .Range("K2:K200").EntireRow.Hidden = False
         Shp.TextFrame.Characters.Caption = "Hide"
      End If
      .Protect , True, True, True
      .EnableSelection = xlUnlockedCells
   End With
End Sub
 
Upvote 0
FANTASTIC! It works like a charme, Thank you Fluff! You know... without you guys I am nothing!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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