Hide rows not containing a word

Keith_C

New Member
Joined
May 22, 2019
Messages
2
Hello, this is going to be a slightly long question. I will try to be as detailed as possible.

I have a workbook with data, and column F contains a long string of text, which is essentially a description. Column E contains either one of two identifiers, "Main" or "Related". When a "Related" lies directly beneath a "Main", the two are considered to be bound as a group.

1. A single "Main" can have any number of "Related" rows bound to it. This is a group.
2. A "Main" can exist as a standalone entity. This is also considered a group.
3. A "Related" cannot exist without being bound to a "Main".

I would like to be able to hide all groups that do not have a particular word "WORD1" in the description (Column F) of the "Main" that is associated with the group. Using the below table as an example, if only Description4 contains my chosen word "WORD1", then I would want to hide rows 1-3, 8-10.

Preferably this word can be entered into a cell, and I can repeat the process with other words (WORD2, WORD3 etc.).

Any help would be immensely appreciated.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Main[/TD]
[TD]Description1[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Main[/TD]
[TD]Description2[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Related[/TD]
[TD]Description3[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Main[/TD]
[TD]Description4[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Related
[/TD]
[TD]Description5[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Related[/TD]
[TD]Description6[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Related[/TD]
[TD]Description7[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Main[/TD]
[TD]Description8[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Main[/TD]
[TD]Description9[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Main[/TD]
[TD]Description10[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited by a moderator:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Let's do it using cell G1 as the criteria cell
Code:
ub t()
Dim crt As String, i As Long
With ActiveSheet
    crt = .Range("G1").Value
    For i = 2 To .Cells(Rows.Count, "F").End(xlUp).Row
        If InStr(.Cells(i, 6).Value, crt) > 0 Then
            x = i + 1
            Do While LCase(.Cells(x, 5).Value) <> "main"
                Rows(x).Hidden = True
                x = x + 1
            Loop
            .Rows(i).Hidden = True
        End If
    Next
End With
End Sub
 
Upvote 0
To be automatic and every time you change the data in cell G1. Put the following code in the events of your sheet.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("G1")) Is Nothing Then
        If Target.Count > 1 Then Exit Sub
        
        Application.ScreenUpdating = False
        If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
        lr = Range("F" & Rows.Count).End(xlUp).Row
        With Range("G2:G" & lr)
            .Formula = "=IF(RC[-2]=""Main"",IF(IFERROR(SEARCH(R1C7,RC[-1]),0)>0,1,0),IF(R[-1]C=1,1,0))"
            .Value = .Value
        End With
        ActiveSheet.Range("A1:G" & lr).AutoFilter Field:=7, Criteria1:="=1"
        Range("G2:G" & lr).ClearContents
    End If


End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
 
Upvote 0
Hi Whiz, Dante,

Thank you both very much for taking the time to help me. I ended up using Dante's code which worked brilliantly.
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,111
Members
452,544
Latest member
aush

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