Click Checkbox to Hide and Unhide Rows

Glasgowsmile

Active Member
Joined
Apr 14, 2018
Messages
280
Office Version
  1. 365
Platform
  1. Windows
I want to create a checkbox that will hide and unhide a specific row based on the value of another cell.

Sheet10 Data (Column K):
A
A1
A2
A3

Sheet9 Data (Column B):
A1
A2
A
A3

In this example I click a checkbox next to A on Sheet10 and it looks at the 'A' value then goes to find it in Sheet9 because the location in Column B isn't always going to be the same. If it finds it and the column isn't hidden then it hides it. This part of the code works... what doesn't work is the opposite, when you uncheck the box it doesn't unhide the value.

Instead I get a run-time error '91': Object variable or With block variable not set

VBA Code:
Sub CheckBox3_Click()
Dim FindString As String
Dim Rng As Range
FindString = Sheet10.Range("K2").Value
    With Sheet9.Range("B:B") 'searches all of column B
        Set Rng = .Find(What:=FindString, _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
        If Not Rng Is Nothing And Rng.EntireRow.Hidden = False Then
            Rng.EntireRow.Hidden = True
        ElseIf Rng.EntireRow.Hidden = True Then
            Rng.EntireRow.Hidden = False
        End If
    End With
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I tried changing the xlValues to xlFormulas within the Find so apparently it cannot Find in hidden rows but then the checkbox doesn't work at all because the "B:B" range is filled with Formulas and it's not the same formula used in the FindString...
 
Upvote 0
Try the following instead . . .

VBA Code:
        If Not Rng Is Nothing Then
            If Rng.EntireRow.Hidden = False Then
                Rng.EntireRow.Hidden = True
            Else
                Rng.EntireRow.Hidden = False
            End If
        End If

Actually, it can be shortened as follows . . .

VBA Code:
        If Not Rng Is Nothing Then
            Rng.EntireRow.Hidden = (Not Rng.EntireRow.Hidden)
        End If

Hope this helps!
 
Upvote 0
Try the following instead . . .

VBA Code:
        If Not Rng Is Nothing Then
            If Rng.EntireRow.Hidden = False Then
                Rng.EntireRow.Hidden = True
            Else
                Rng.EntireRow.Hidden = False
            End If
        End If

Actually, it can be shortened as follows . . .

VBA Code:
        If Not Rng Is Nothing Then
            Rng.EntireRow.Hidden = (Not Rng.EntireRow.Hidden)
        End If

Hope this helps!

Sadly the unhide doesn't work, I guess because the find functionality cannot look at hidden rows?
 
Upvote 0
Actually, Find will work within manually hidden rows. It just won't work within data which isn't visible after applying the filter.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

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