Hide all rows with the same values as hidden rows - excel vba

CBC

New Member
Joined
Mar 5, 2020
Messages
3
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a worksheet of hidden rows that I would like to hide more rows from. The desired function is written as "If a non-hidden rows value (in column C) is equal to the value of any hidden row's column C value, then hide that non-hidden row."

The first function that I wrote (that works to hide rows based on task in column G) is:
Sub HideCompletes()
For Each cell In ActiveSheet.Range("G5:G200")
If cell.Value = "Completed" Or cell.Value = "" Then
cell.EntireRow.Hidden = True
End If
Next cell
End Sub

The follow up function and hide I want to do based on those values is the part that I cannot seem to write, this is the very wrong piece I have:
'Column C is full of names, I need to hide the rows with names that match the hidden rows names

Sub RemoveMultipleEntries()
For Each cell.EntireRow.Hidden = True In ActiveSheet.Range("C5:C200")
If cell.EntireRow.Hidden = False And In Range("C5:C200").Value Then
cell.EntireRow.Hidden = True
End If
Next cell
End Sub

'I feel as though I need multiple in statements to accomplish this but can not wrap my head around how this needs to be written from the first variable in the for statement.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Something like this perhaps
VBA Code:
Sub RemoveMultipleEntries()

    Dim raTmp       As Range
    Dim c           As Range
    Dim i           As Long

    Set raTmp = ActiveSheet.Range("C5:C200")

    For Each c In raTmp
        If c.EntireRow.Hidden = True Then
            For i = 1 To raTmp.Count
                If c.Text = raTmp.Cells(1, i).Text Then
                    If raTmp.Cells(1, i).EntireRow.Hidden = True Then
                        c.EntireRow.Hidden = True
                    End If
                End If
            Next i
        End If
    Next c

End Sub
 
Upvote 0
Sorry, do forget my post #2 because of a moment of inattention ...
VBA Code:
Sub RemoveMultipleEntries()

    Dim raTmp       As Range
    Dim c           As Range
    Dim i           As Long

    Set raTmp = ActiveSheet.Range("C5:C200")

    For Each c In raTmp
        If c.EntireRow.Hidden = False Then
            For i = 1 To raTmp.Count
                If c.Text = raTmp.Cells(1, i).Text Then
                    If raTmp.Cells(1, i).EntireRow.Hidden = True Then
                        c.EntireRow.Hidden = True
                    End If
                End If
            Next i
        End If
    Next c
End Sub
 
  • Like
Reactions: CBC
Upvote 0
Thanks for your response GWteB! Sorry for the basic questions, this looks right, but I'm very new to VBA and this isnt working for me for some reason. The values in column C are text, and I saw that you defined i as Long, will that work with text as you've written it? You're far more advanced at this than I, so I'm strill trying to wrap my head around some of the concepts there. Thanks!
 
Upvote 0
In the line
VBA Code:
If c.Text = raTmp.Cells(1, i).Text Then
i is the placeholder for a number that can change; it represents a column number......(oops, my 2nd mistake).
Have that said, change those two lines to:
VBA Code:
    If c.Text = raTmp.Cells(i,1).Text Then
        If raTmp.Cells(i, 1).EntireRow.Hidden = True Then
 
  • Like
Reactions: CBC
Upvote 0
Ah I see, that makes perfect sense then. I'll have to dive into the why's a little bit more but it works perfectly! Thank you so much!
 
Upvote 0
You are welcome and thanks for letting me know.
 
Upvote 0

Forum statistics

Threads
1,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

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