Toggle Button to Hide/Unhide column based on specific text in rows

onthegreen03

Board Regular
Joined
Jun 30, 2016
Messages
148
Office Version
  1. 365
Platform
  1. Windows
Hi All -

I want to add a Toggle Button, that when clicked will search all rows in column C of my spreadsheet and hide the rows which contain the word Vacant. Obviously, when the button is clicked again it will unhide all rows that contain the word Vacant. Can anyone send me the VBA code that will get this done? I've looked around on line and even tried a few times and can't seem to figure it out. Thanks in advance!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try this:
VBA Code:
Private Sub ToggleButton1_Click()
'Modified 6/29/2022  12/31/1899 12:01:46 AM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "C").End(xlUp).Row

For i = 1 To Lastrow
    If Cells(i, 3).Value = "Vacant" Then Rows(i).Hidden = Not Rows(i).Hidden
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
This script will run the same if it's in a Toggle Button or a Command button
 
Upvote 0
This works like a charm, thank you! Would you be able to modify the code slightly so I can add captions when hidden and unhidden? When hidden I would like the button to read "Unhide Vacant", and when unhidden I would like "Hide Vacant". Thanks again!
 
Upvote 0
This works like a charm, thank you! Would you be able to modify the code slightly so I can add captions when hidden and unhidden? When hidden I would like the button to read "Unhide Vacant", and when unhidden I would like "Hide Vacant". Thanks again!
This script runs through a range of cells to see if they equal "Vacant" or not.
So, if we are checking 100 rows to see if the value= "Vacant" then in one row it would be true but the next row it may not be true
So that would mean the Caption on the Toggle Button would be going from Hidden to Not Hidden maybe 50 times almost instantly back and forth. Not sure why you would need that. It's not like checking one cell
 
Upvote 0
Here is an example of how A Toggle Button works.
VBA Code:
Private Sub ToggleButton1_Click()
'Modified  6/30/2022  12:53:14 PM  EDT
If ToggleButton1.Value = True Then Range("A1").Value = "Alpha": ToggleButton1.Caption = "Alpha"
If ToggleButton1.Value = False Then Range("A1").Value = "Bravo": ToggleButton1.Caption = "Bravo"
End Sub
 
Upvote 0
I was trying to add the same functionality as my other toggle buttons on the sheet (see the code below which I am using to hide/unhide certain columns). I like that the caption automatically changes depending on if the button is pressed or not. No worries, I can just skip it. Thanks again!

VBA Code:
Private Sub ToggleButton1_Click()
Dim xAddress As String
xAddress = "G:K"
If ToggleButton1.Value Then
    Application.ActiveSheet.Columns(xAddress).Hidden = True
    ToggleButton1.Caption = "Show Q1"
Else
    Application.ActiveSheet.Columns(xAddress).Hidden = False
    ToggleButton1.Caption = "Hide Q1"
End If
End Sub
 
Upvote 0
OK I have figured out how to do this:
Using A Toggle Button:
Try this:
VBA Code:
Private Sub ToggleButton1_Click()
'Modified  6/30/2022  1:39:11 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "C").End(xlUp).Row
    
    If ToggleButton1.Value = True Then
        ToggleButton1.Caption = "Hidden"
            For i = 1 To Lastrow
                If Cells(i, 3).Value = "Vacant" Then Rows(i).Hidden = True
            Next
    End If


    If ToggleButton1.Value = False Then
        ToggleButton1.Caption = "Not Hidden"
        For i = 1 To Lastrow
            If Cells(i, 3).Value = "Vacant" Then Rows(i).Hidden = False
        Next
    End If
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,812
Messages
6,121,699
Members
449,048
Latest member
81jamesacct

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