# Click Checkbox to Hide and Unhide Rows



## Glasgowsmile (Dec 19, 2022)

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


```
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
```


----------



## Glasgowsmile (Dec 19, 2022)

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...


----------



## Domenic (Dec 19, 2022)

Try the following instead . . .


```
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 . . .


```
If Not Rng Is Nothing Then
            Rng.EntireRow.Hidden = (Not Rng.EntireRow.Hidden)
        End If
```

Hope this helps!


----------



## Glasgowsmile (Dec 20, 2022)

Domenic said:


> Try the following instead . . .
> 
> 
> ```
> ...



Sadly the unhide doesn't work, I guess because the find functionality cannot look at hidden rows?


----------



## Domenic (Dec 20, 2022)

Actually, Find will work within manually hidden rows.  It just won't work within data which isn't visible after applying the filter.


----------

