PDA

View Full Version : [SOLVED:] Showing & Hiding Rows based on values



Sully1440
01-21-2019, 07:48 AM
Hi,
I created a toggle button to hide rows based on a value. I found some code for this and it works great. However, I can't make it show rows looping through the hidden rows. Basically, I want to do the opposite as the first part of the code.

Here is the code:


Private Sub ToggleButton2_Click()
Dim rg As Range, c As Range
Dim firstAddress As String
Set rg = Range("B4", Cells(Cells.Rows.Count, "B").End(xlUp))
'Application.ScreenUpdating = False

If ToggleButton2.Value = True Then
With rg
Set c = .Find(what:="Complete", lookat:=xlWhole, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.EntireRow.Hidden = True
Set c = .FindNext(c)
If c Is Nothing Then Exit Do
Loop While c.Address <> firstAddress
End If
Application.ScreenUpdating = True
End With
Range("M1").Select
Else
Cells.Select
Selection.EntireRow.Hidden = False
Range("M1").Select
End If
End Sub

Sully1440
01-22-2019, 06:37 AM
In other words, I'm looking for a way to show rows that were previously hidden from the first if statement (based on "Completed" in a certain column). I have other hidden rows that have different values that I do not want shown, so it's only based on the value "Completed" in column B.


Private Sub ToggleButton2_Click()
Dim rg As Range, c As Range
Dim firstAddress As String
Set rg = Range("B4", Cells(Cells.Rows.Count, "B").End(xlUp))
'Application.ScreenUpdating = False

If ToggleButton2.Value = True Then
With rg
Set c = .Find(what:="Complete", lookat:=xlWhole, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.EntireRow.Hidden = True
Set c = .FindNext(c)
If c Is Nothing Then Exit Do
Loop While c.Address <> firstAddress
End If
Application.ScreenUpdating = True
End With
Range("M1").Select
Else

'************************************************************************** ************************************************
' THIS SECTION IS WHERE I'D LIKE SOME HELP.
' I'M TRYING TO SHOW ROWS PREVIOUSLY HIDDEN FROM THE CODE ABOVE IN THE IF STATEMENT BUT ONLY FOR "COMPLETED" IN COLUMN B

Cells.Select
Selection.EntireRow.Hidden = False
Range("M1").Select
'************************************************************************** *************************************************
End If
End Sub

Paul_Hossler
01-22-2019, 07:45 AM
If I understood, this fragment should work. Try the attachment with has hidden rows with "Completed" and some hidden rows with something different



Option Explicit


Sub UnhideCompleted()
Dim r As Range, c As Range

For Each c In Range("B4:B30").Cells
If c.Value = "Completed" And c.EntireRow.Hidden Then
c.EntireRow.Hidden = False
End If
Next
End Sub

Sully1440
01-22-2019, 09:42 AM
Works perfectly. Thank you Paul :)