Macro to filter showing upper levels

thc57e

New Member
Joined
Nov 21, 2016
Messages
4
Hello folks!

I am creating an action list, organised by clusters, with 3 different levels - level 1 (1), level 2 (1.1) and level 3 (1.1.1) -

I would like to filter the user (assigned only to actions in level 3) in a way that it will also show me above the level 2 and level 1 corresponding to it, so it is easier to track it.

I will try to illustrate it. This is the original list:

[TABLE="class: grid, width: 490"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]ID[/TD]
[TD]Level[/TD]
[TD]User[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]1.0.1[/TD]
[TD]3[/TD]
[TD]User A[/TD]
[/TR]
[TR]
[TD]1.1[/TD]
[TD]2[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]1.1.1[/TD]
[TD]3[/TD]
[TD]User B[/TD]
[/TR]
[TR]
[TD]1.1.2[/TD]
[TD]3[/TD]
[TD]User C[/TD]
[/TR]
[TR]
[TD]1.1.3[/TD]
[TD]3[/TD]
[TD]User A[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]1[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2.1[/TD]
[TD]2[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2.1.1[/TD]
[TD]3[/TD]
[TD]User B[/TD]
[/TR]
[TR]
[TD]2.1.2[/TD]
[TD]3[/TD]
[TD]User A[/TD]
[/TR]
[TR]
[TD]2.2[/TD]
[TD]2[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2.2.1[/TD]
[TD]3[/TD]
[TD]User B[/TD]
[/TR]
[TR]
[TD]2.2.2[/TD]
[TD]3[/TD]
[TD]User A[/TD]
[/TR]
[TR]
[TD]2.2.3[/TD]
[TD]3[/TD]
[TD]User C[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]1[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]3.0.1[/TD]
[TD]3[/TD]
[TD]User B[/TD]
[/TR]
[TR]
[TD]3.0.2[/TD]
[TD]3[/TD]
[TD]User A[/TD]
[/TR]
</tbody>[/TABLE]

and this should be the result if I filter by USER A:

[TABLE="class: grid, width: 546"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]ID[/TD]
[TD]Level[/TD]
[TD]User[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]1.0.1[/TD]
[TD]3[/TD]
[TD]User A[/TD]
[/TR]
[TR]
[TD]1.1[/TD]
[TD]2[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]1.1.3[/TD]
[TD]3[/TD]
[TD]User A[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]1[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2.1[/TD]
[TD]2[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2.1.2[/TD]
[TD]3[/TD]
[TD]User A[/TD]
[/TR]
[TR]
[TD]2.2[/TD]
[TD]2[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2.2.2[/TD]
[TD]3[/TD]
[TD]User A[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]1[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]3.0.2[/TD]
[TD]3[/TD]
[TD]User A[/TD]
[/TR]
</tbody>[/TABLE]


Thank you very much in advance.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this:-
Load code into sheet module.
To run code and Filter by Selected value, double click any value in column "C".
To Un-filter , right click anywhere in column "A".

Code:
Private [COLOR="Navy"]Sub[/COLOR] Worksheet_BeforeDoubleClick(ByVal Target [COLOR="Navy"]As[/COLOR] Range, Cancel [COLOR="Navy"]As[/COLOR] Boolean)
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]If[/COLOR] Target.Column = 3 And Target.Count = 1 [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]Set[/COLOR] Rng = Range(Range("C2"), Range("C" & Rows.Count).End(xlUp))
        [COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
            .CompareMode = vbTextCompare
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
        [COLOR="Navy"]If[/COLOR] Not Dn.Value = "" [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] Not .exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
                .Add Dn.Value, Dn
            [COLOR="Navy"]Else[/COLOR]
                [COLOR="Navy"]Set[/COLOR] .Item(Dn.Value) = Union(.Item(Dn.Value), Dn)
            [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
[COLOR="Navy"]If[/COLOR] .exists(Target.Value) [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]If[/COLOR] Not K = Target.Value [COLOR="Navy"]Then[/COLOR]
    .Item(K).EntireRow.Hidden = True
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Private [COLOR="Navy"]Sub[/COLOR] Worksheet_BeforeRightClick(ByVal Target [COLOR="Navy"]As[/COLOR] Range, Cancel [COLOR="Navy"]As[/COLOR] Boolean)
[COLOR="Navy"]If[/COLOR] Target.Column = 1 And Target.Count = 1 [COLOR="Navy"]Then[/COLOR]
    Cancel = True
    ActiveSheet.Rows.Hidden = False
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:-
Load code into sheet module.
To run code and Filter by Selected value, double click any value in column "C".
To Un-filter , right click anywhere in column "A".

Code:
Private [COLOR=Navy]Sub[/COLOR] Worksheet_BeforeDoubleClick(ByVal Target [COLOR=Navy]As[/COLOR] Range, Cancel [COLOR=Navy]As[/COLOR] Boolean)
[COLOR=Navy]Dim[/COLOR] Rng [COLOR=Navy]As[/COLOR] Range, Dn [COLOR=Navy]As[/COLOR] Range, Temp [COLOR=Navy]As[/COLOR] [COLOR=Navy]String[/COLOR]
[COLOR=Navy]Dim[/COLOR] K [COLOR=Navy]As[/COLOR] Variant, c [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]If[/COLOR] Target.Column = 3 And Target.Count = 1 [COLOR=Navy]Then[/COLOR]
    [COLOR=Navy]Set[/COLOR] Rng = Range(Range("C2"), Range("C" & Rows.Count).End(xlUp))
        [COLOR=Navy]With[/COLOR] CreateObject("scripting.dictionary")
            .CompareMode = vbTextCompare
    [COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] Dn [COLOR=Navy]In[/COLOR] Rng
        [COLOR=Navy]If[/COLOR] Not Dn.Value = "" [COLOR=Navy]Then[/COLOR]
            [COLOR=Navy]If[/COLOR] Not .exists(Dn.Value) [COLOR=Navy]Then[/COLOR]
                .Add Dn.Value, Dn
            [COLOR=Navy]Else[/COLOR]
                [COLOR=Navy]Set[/COLOR] .Item(Dn.Value) = Union(.Item(Dn.Value), Dn)
            [COLOR=Navy]End[/COLOR] If
    [COLOR=Navy]End[/COLOR] If
    [COLOR=Navy]Next[/COLOR]
[COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] K [COLOR=Navy]In[/COLOR] .keys
[COLOR=Navy]If[/COLOR] .exists(Target.Value) [COLOR=Navy]Then[/COLOR]
    [COLOR=Navy]If[/COLOR] Not K = Target.Value [COLOR=Navy]Then[/COLOR]
    .Item(K).EntireRow.Hidden = True
    [COLOR=Navy]End[/COLOR] If
[COLOR=Navy]End[/COLOR] If
[COLOR=Navy]Next[/COLOR] K
[COLOR=Navy]End[/COLOR] With
[COLOR=Navy]End[/COLOR] If
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Private [COLOR=Navy]Sub[/COLOR] Worksheet_BeforeRightClick(ByVal Target [COLOR=Navy]As[/COLOR] Range, Cancel [COLOR=Navy]As[/COLOR] Boolean)
[COLOR=Navy]If[/COLOR] Target.Column = 1 And Target.Count = 1 [COLOR=Navy]Then[/COLOR]
    Cancel = True
    ActiveSheet.Rows.Hidden = False
[COLOR=Navy]End[/COLOR] If
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick


Hello Mick,

Thank you very very much for your idea. It is almost working. However, it still have an issue.

When I press user C, look what it does:


[TABLE="class: grid, width: 490"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]ID[/TD]
[TD]Level[/TD]
[TD]User[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]1.1[/TD]
[TD]2[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]1.1.2[/TD]
[TD]3[/TD]
[TD]User C[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]1[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2.1[/TD]
[TD]2[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2.2[/TD]
[TD]2[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2.2.3[/TD]
[TD]3[/TD]
[TD]User C[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]1[/TD]
[TD] [/TD]
[/TR]
</tbody>[/TABLE]


It should not show ID 2.1. It should be like this:

[TABLE="class: grid, width: 490"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Level[/TD]
[TD]User[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]1.1[/TD]
[TD]2[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]1.1.2[/TD]
[TD]3[/TD]
[TD]User C[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]1[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2.2[/TD]
[TD]2[/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]2.2.3[/TD]
[TD]3[/TD]
[TD]User C[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]1[/TD]
[TD] [/TD]
[/TR]
</tbody>[/TABLE]

Thank you.
 
Upvote 0
I'm not too sure what you want !!
Would it be in this particular case (same for all cases) of "User C", looking at cells above that cell up to the next "User C" value for the first blank cells That have "Level" 2 and or "1" in column "Level" and including these cells in the range to be Visible.
That is looking up for the first values up of "1" and "2", and if there is a value include with visible range for "User C".
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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