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.

ABCDEF
1MainDescription1
2MainDescription2
3RelatedDescription3
4MainDescription4
5Related
Description5
6RelatedDescription6
7RelatedDescription7
8MainDescription8
9MainDescription9
10MainDescription10

<tbody>
</tbody>
 
Last edited by a moderator:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,748
Members
448,989
Latest member
mariah3

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