Created this toggel button, but it's not working. Please help!

The Phoenix

New Member
Joined
Jul 13, 2015
Messages
3
I wanted to create the following toggle button.
Button would say 'Hide Done' and when clicked, it would hide all rows with the letter 'a' in cell in column B.
Same button would now say 'Show Done', and when clicked again, it would show the rows again.
Here's the code I came up with, but it's not working.
Code:
Private Sub ShowHideDoneClickButton()
ActiveSheet.Unprotect
Dim Cap1 As String, Cap2 As String
Cap1 = "Hide Done"
Cap2 = "Show Done"
Dim cell As Range
    For Each cell In Range("$B:$B")
        If cell.Value = "a" Then
            cell.EntireRow.Hidden = True
    CommandButton1.Caption = Cap1
Else
Application.ScreenUpdating = False
Dim cell As Range
    For Each cell In Range("$B:$B")
        If cell.Value = "a" Then
            cell.EntireRow.Hidden = False
    CommandButton1.Caption = Cap2
End If
ActiveSheet.Protect
End Sub
What am I doing wrong or missing?
This has been driving me crazy for the past couple weeks, and have looked everywhere for an answer.
Thanks.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Your code isn't testing the value of the toggle button. It is doing the same thing every time it is run. Try something like

Code:
Dim cell As Range
Dim Flag As Boolean

With Range("B:B")
    For Each cell In Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
        If cell.Value = "a" Then cell.EntireRow.Hidden = ToggleButton1.Value
    Next cell
End With

ToggleButton1.Caption = IIf(ToggleButton1.Value, "Show rows", "Hide rows")
 
Upvote 0
Post answered in another forum. In case anyone wants to know the answer, here it is:
Create Active X Button
Right Click on 'Properties' and change Caption to 'Hide Done'
Right Click on Button and select 'View Code'

Put in the following code at the top:

Code:
Private Sub CommandButton1_Click()
Dim Cap1 As String, Cap2 As String
Dim cell As Range
Dim lr As Long
ActiveSheet.Unprotect
Cap2 = "Hide Done"
Cap1 = "Show Done"
lr = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
If ActiveSheet.CommandButton1.Caption = Cap2 Then
    For Each cell In Range("B1:B" & lr)
        If cell.Value = "a" Then cell.EntireRow.Hidden = True
    Next cell
    ActiveSheet.CommandButton1.Caption = Cap1
ElseIf ActiveSheet.CommandButton1.Caption = Cap1 Then
    ActiveSheet.Cells.EntireRow.Hidden = False
    ActiveSheet.CommandButton1.Caption = Cap2
End If
End Sub

Works like a charm. Thanks again Arkadi!!!! You're the man!!!!
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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