VBA to automatically hide rows in pivot based on column

Binbs

New Member
Joined
Jan 7, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hello,

I would like to automatically hide a row in my pivot table when a cell in column "Exclude" (column O) is equal to "x".

I currently have the code below which is refreshing my pivot when cells in I7:J7 are updated and it's working beautifully. When I try to include code in bold below to hide rows in column O when cells are equal to "x", it does not work.

Any help is very appreciated.

Sub Worksheet_Change(ByVal Target As Range)

Dim KeyCells As Range

Set KeyCells = Range("I7:J7")

If Not Application.Intersect(KeyCells, Target) Is Nothing Then

Worksheets("CST View").PivotTables("AutoOL").PivotCache.Refresh

ElseIf Range("O:O") = "x" Then
Rows("O:O").EntireRow.Hidden = True


End If
End If
End Sub

Thank you
 
if you reorganize your pivottable and you place your exclusive as 1st field in the rowrange and unselect it, you only have to refresh the table and everything is ok. I think you make it too complicated.
I'll make an example ....

left table with data and a column to make rows exclusive.
2 pivottables, the 1st with exclusive in the filterarea, left top, and i filtered so that the "x" isn't shown.
The 2nd has exclusive as first item and you have a x-part and an empty part.
piece of a cake, which fits best ?


Map1
ABCDEFGHI
1itemqtyexclusiveexclusive(leeg)
2a5x
3b4RijlabelsSom van qty
4c3c3
5d2e1
6e1b4
7d2
8Eindtotaal10
9
10
11
12RijlabelsSom van qty
13x5
14a5
15
16(leeg)10
17c3
18e1
19b4
20d2
21
22Eindtotaal15
23
Blad1
 
Last edited:
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
if you reorganize your pivottable and you place your exclusive as 1st field in the rowrange and unselect it, you only have to refresh the table and everything is ok. I think you make it too complicated.
I'll make an example ....

left table with data and a column to make rows exclusive.
2 pivottables, the 1st with exclusive in the filterarea, left top, and i filtered so that the "x" isn't shown.
The 2nd has exclusive as first item and you have a x-part and an empty part.
piece of a cake, which fits best ?


[I wish it were this simple. If I understand this solution, it would be possible for a user to mistakenly "Clear filters" and view items marked with an "x" which would be problematic.
 
Upvote 0
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   
     Set c0 = Intersect(Target, Range("F:F"))                   'intersect target with column F
     Set c1 = Intersect(Target, Range("AQ:AQ"))                 'intersect target with column AQ

     If Not c0 Is Nothing Or Not c1 Is Nothing Then             'something changed in 1 of these columns
          With Worksheets("CST View")                           'this worksheet
               Set pvt = .PivotTables("AutoOL")                 'this pivottable
               pvt.PivotCache.Refresh                           'refresh
               Set c2 = Intersect(pvt.RowRange, .Columns("O"))  'intersect of column O and the PVT rowrange
               For Each c In c2.Cells                           'loop through all those cells in the pivottable
                    c2.EntireRow.Hidden = (c2.Value <> "")      'hide or unhide row if cell is empty or not
               Next
          End With
     End If
End Sub
 
Upvote 0
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  
     Set c0 = Intersect(Target, Range("F:F"))                   'intersect target with column F
     Set c1 = Intersect(Target, Range("AQ:AQ"))                 'intersect target with column AQ

     If Not c0 Is Nothing Or Not c1 Is Nothing Then             'something changed in 1 of these columns
          With Worksheets("CST View")                           'this worksheet
               Set pvt = .PivotTables("AutoOL")                 'this pivottable
               pvt.PivotCache.Refresh                           'refresh
               Set c2 = Intersect(pvt.RowRange, .Columns("O"))  'intersect of column O and the PVT rowrange
               For Each c In c2.Cells                           'loop through all those cells in the pivottable
                    c2.EntireRow.Hidden = (c2.Value <> "")      'hide or unhide row if cell is empty or not
               Next
          End With
     End If
End Sub
This looks good! Do you know how I can get around this type mismatch error that is popping up when I run the code? Is it happening with this line:

1641790172703.png

1641790200682.png


Thanks very much for your help.
 
Upvote 0
with a msgbox, you see the address of c2 (is for tempory use of course)
If you check that range, is there something special (error, ...) in 1 of those cells ?
With len(c2.value) you check now the length of the values in the cells
VBA Code:
Set c2 = Intersect(pvt.RowRange, .Columns("O"))                 'intersect of column O and the PVT rowrange
MsgBox c2.Address
For Each c In c2.Cells                                          'loop through all those cells in the pivottable
     c2.EntireRow.Hidden = (Len(c2.Value) > 0)                   'hide or unhide row if cell is empty or not
Next
 
Upvote 0
with a msgbox, you see the address of c2 (is for tempory use of course)
If you check that range, is there something special (error, ...) in 1 of those cells ?
With len(c2.value) you check now the length of the values in the cells
VBA Code:
Set c2 = Intersect(pvt.RowRange, .Columns("O"))                 'intersect of column O and the PVT rowrange
MsgBox c2.Address
For Each c In c2.Cells                                          'loop through all those cells in the pivottable
     c2.EntireRow.Hidden = (Len(c2.Value) > 0)                   'hide or unhide row if cell is empty or not
Next


With the msgbox, I see the address is $O$20:$O$794. That looks correct. Then I get the type mismatch error again.

I checked the range and there is nothing that looks unusual to me such as an error. Just (blank) or x.
 
Upvote 0
very stupid error, c2 instead of c
VBA Code:
For Each c In c2.Cells                                          'loop through all those cells in the pivottable
     c.EntireRow.Hidden = (Len(c.Value) > 0)                   'hide or unhide row if cell is empty or not
Next
 
Upvote 0
very stupid error, c2 instead of c
VBA Code:
For Each c In c2.Cells                                          'loop through all those cells in the pivottable
     c.EntireRow.Hidden = (Len(c.Value) > 0)                   'hide or unhide row if cell is empty or not
Next
Thanks so much for all your help!
 
Upvote 0
I found that the last code would hide all columns, not just those listed with an "x" for some reason.

I changed the value to -1 instead of x and used this:

VBA Code:
For Each c In c2.Cells                                          'loop through all those cells in the pivottable
     c.EntireRow.Hidden = (c.Value) < 0                         'hide or unhide row if cell is empty or not
Next

This works great and hides only the rows I need to hide. However, anytime I filter or use a slicer, the hidden rows are unhidden. When I remove the filter or slicer, they are hidden again. Any idea why this happens?

Thank you
 
Upvote 0
Solution
excel tries to be smarter, to be helpfull and most of the time it's oké, but here not.
you have to correct it with the Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable) - event.
You have to write exact the same as in the other event, or make 1 macro and call him from each event.
 
Upvote 0

Forum statistics

Threads
1,215,035
Messages
6,122,785
Members
449,095
Latest member
m_smith_solihull

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