using VBA to hide rows in a pivot table

gayjaybird

New Member
Joined
Aug 6, 2007
Messages
39
Code:
Sub Hidesingles()
  For i = 5 To ActiveSheet.UsedRange.Rows.Count
      For c = 3 To ActiveSheet.UsedRange.Column.Count
        If Cells(i, c) <= 1 Then Cells(i, 1).EntireRow.Hidden = True Else: Next c
        End If
Next i
End Sub

I'm trying to filter a pivot table with VBA. The spreadsheet/database I'm summarizing has fields for the store name, the package tracking number, the date and the sender's store name. Row headings are store name and tracking number, and the columns are the date. The pivot table counts how many times a particular sender's store name shows up on a particular date. I have the rows collapsed so that the table only displays the subtotals for each store for each day. How can I filter the table so that only the stores who sent more than one package a day are listed? I tried the code above and ended up with a 'Next without For error.' Any ideas?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Code:
Sub Hidesingles()
For i = 5 To ActiveSheet.UsedRange.Rows.Count
    For c = 3 To ActiveSheet.UsedRange.Column.Count
        If Cells(i, c)<= 1 Then
            Cells(i, 1).EntireRow.Hidden = True
        End If
    Next c
Next i
End Sub

You'd be better off not putting dates in columns...256 columns (Excel 2003) is only enough for a year or less of data. Even if you have more columns (Excel 2007) the design makes it awkward.

This would make a good dataset that a pivot table could summarize in a jif:
book2
ABCDE
1StoreTrkIDDateSenderShipCount
2Store1XYD11322-SepSender13
3Store1XYD11422-SepSender23
4Store1XYD11522-SepSender33
5Store2XYD11623-SepSender41
6Store3XYD11723-SepSender52
7Store3XYD11823-SepSender62
8Store3XYD11924-SepSender71
9Store4XYD12024-SepSender82
10Store4XYD12124-SepSender92
11Store5XYD12224-SepSender101
Sheet1


The last formula is an array formula, therefore entered with Control + Shift + Enter instead of just Enter.

HTH[/code]
 
Upvote 0
I forgot to mention, with the dataset as above you can just add a page field to your pivot table report -- therefore easily hide stores with only one package on the day. You could also group by stores, by date ranges, by package counts, and of course get your totals...by any of the above groups or by sum totals.

Regards.
 
Upvote 0
Thanks, that's a start. Actually, the date issue isn't such a big deal, because this spreadsheet only covers one billing period-- 12 to 14 days, if I recall. They query our business system and it spits out a report in excel format. They want to condense this report so they can bill the stores who send more than one shipment per day.

Here's a screenshot of my dummy pivot table
multipleordersdummyscreenshot.jpg


I want to hide the information for Stores A and D, and display the information for the companies with the highlights. This is just a dummied version for demonstration. The actual Pivot table can expand and collapse the details for each store so users can see subtotals or view the individual tracking numbers.

Does this make any more sense?

Thanks.
 
Upvote 0
Along with your pivot, please post the source data that this pivot table is based on, if possible...

Some thoughts off the top of my head:
I am not sure there is any way to dynamically hide the stores you don't want visible. You can hide row fields...but you would have to select the rows to hide and I think you want it done more automatically. Same (even more so) for data fields...you could hide the columns you don't want visible using pivot table options, but it needs to be "smarter" for you purposes.

You could add a column to your data source where you can use a formula to "flag" (true/false or one/zero) stores that have more than one shipment on a day...This could be your page field to show the stores. But with the columns...I am not so sure how you'd filter out the dates you don't want...Stumped. Plus, you have the problem that the same data might be a 1 shipment for one store and a 2 shipments for another store, so in fact you'd need to "show" that column for the second store, even if you wanted to "hide" it for the first...so I am thinking you'd need to show all dates, and then somehow sum the extra shipping. This would be easy "outside" the pivot table, but I'll have to scratch my head some more to think if we can do it "inside" the pivot table.

Anyway, if you'd like to post a set of data that this pivot table is built on (enough to show the relevant fields and the types of data they contain) we can probably work at that level to craft the proper dataset and/or build the results table we need.

As usual in cases such as this, a query-based solution with Microsoft Access or MS Query would demonstrate the power of a simple SQL statement! But we can work a solution with Excel--it just takes a little more work.

Regards.
 
Upvote 0
Here's one rather pathetic try :lol: I just added a column of 1's to the data table...so you can do addition and subtraction on the number of packages. In this case, the formula only works when the dates are on the table, so if you are expanding the detail and collapsing it this wouldn't work. I am back to thinking lets look at the source data and maybe work there...I usually use pivot tables for very specific purposes, so for instance if I was using a pivot table in a case like this I'd set it up to give me the info I need and then leave it alone...if you need a table with drill downs for other users etc., just build another pivot table report for that functionality. I hope this isn't bad advice! The advantage to having one table for your package counts is that you can then put some formulas "outside" the table and the structure remains stable so you can use these formulas consistently.
Pivot.xls
ABCD
3Data
4StoreDateCountofTrackIDExtraShipments
5StoreA2-Sep21
6StoreB3-Sep10
76-Sep10
88-Sep10
9StoreC4-Sep10
107-Sep10
119-Sep32
Sheet4
 
Upvote 0
I'll get the base data for you on Monday when I'm back at work. As far as filtering dates, I'm not so concerned about it, exactly for the reason you mentioned--store A might send one package on 2-Sep, but store B sends 2.

Thanks for your help!
 
Upvote 0
sample data
multiple orders dummy.xls
ABCD
1TrackingNumberReference1PickupDateSenderCompanyName
2ab12374StoreA#1548302007StoreA#154
3ab12375StoreA#1558302007StoreA#155
4ab12376StoreA#1608302007StoreA#160
5ab12377StoreA#1838302007StoreA#183
6ab12378StoreA#1858302007StoreA#185
7ab12379StoreA#1908302007StoreA#190
8ab12380StoreA#1918302007StoreA#191
9ab12358StoreA#21538292007StoreA#2153
10ab12347StoreA#23258222007StoreA#2325
11ab12345StoreA#2708202007StoreA#270
12ab12359StoreA#438292007StoreA#43
13ab12348StoreA#448222007StoreA#44
14ab12360StoreA#4798292007StoreA#479
15ab12352StoreA#6588232007StoreA#658
16ab12361StoreA#8588292007StoreA#858
17ab12353StoreB#10168282007StoreB#1016
18ab12354StoreB#10168282007StoreB#1016
19ab12355StoreB#12678282007StoreB#1267
20ab12356StoreB#3178282007StoreB#317
21ab12346StoreB#3228202007StoreB#322
22ab12349StoreB#518222007StoreB#51
23ab12350StoreB#5758222007StoreB#575
24ab12351StoreB#7238222007StoreB#723
dummy data


Here's some sample data from the actual report, with confidential information changed. In this sample, only the count for Store B #1016 should show up when I run the macro.
 
Upvote 0
It appears to me that you would benefit from a column in your source data to "count" - the problem with pivots is that you can't filter on the data field, so the count has to be rigged up otherwise.

Here's one way:
20071001_packages.xls
ABCDE
1TrackingNumberReference1PickupDateSenderCompanyNameCount
2ab12374StoreA#1548302007StoreA#1541
3ab12375StoreA#1558302007StoreA#1551
4ab12352StoreA#6588232007StoreA#6581
5ab12361StoreA#8588292007StoreA#8581
6ab12353StoreB#10168282007StoreB#10162
7ab12354StoreB#10168282007StoreB#10162
8ab12355StoreB#12678282007StoreB#12671
9ab12356StoreB#3178282007StoreB#3171
10ab12346StoreB#3228202007StoreB#3221
Sheet1


Formula in Cell E2 is
=SUM(($C$2:$C$10000=C2)*($D$2:$D$10000=D2)*1)
Entered as an array formula with Control + Shift + Enter, not just Enter, and assuming 10,000 rows is plenty to cover all your data that will ever be there.

see next post for pivot...
 
Upvote 0
Now, pivot table is easy:
You add a page field at the top, right click on it, select field settings..., and Hide the 1's and Blanks.
For the row fields I've removed subtotals. I also removed Grand Total for Columns from the table options.
20071001_packages.xls
ABCDE
1Count(MultipleItems)
2
3CountofSenderCompanyName
4Reference1TrackingNumberPickupDateTotal
5StoreB#1016ab1235382820071
6ab1235482820071
7
Sheet4
 
Upvote 0

Forum statistics

Threads
1,225,444
Messages
6,185,008
Members
453,273
Latest member
Jeff Wyrick

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