Visual Basic for Applications (VBA)


How to get the List of Visible Items from a Pivot Field using VBA


I'm using the below code to loop through a pivot field for getting the pivot items which are visible. But the pivotitem.count gives a 0 when the pivot field is in Row Label When I move this
pivot field to Column Label, the code is working fine. But I NEED this Field to stay row label. Is there any work around for my problem?
Dim pt As PivotTable
Dim pf As PivotField
Dim pvtitem As PivotItem
Set nwSheet = Worksheets.Add
nwSheet.Activate
rw = 0
Set pt = Sheets("Reasons").PivotTables("PivotFields")
Set pf = pt.PivotFields("[Characteristics].[Reason].[Reason]")
With pf
For i = 0 To .PivotItems.Count
rw = rw + 1
nwSheet.Cells(rw, 1).Value = .PivotItems.Count
Next i
End With
Pramod
Hello,
I can't see you're adressing your rowfields.
I think you got just the pivotField, but not in state as a rowfield, so maybey that's the reason why you have no count?
Maybey you could try something like:
Dim pi As PivotItemFor Each pi In pt.RowFields(1).PivotItems If pi.Visible = True Then 'do your thing Debug.Print pi.Value End IfNext
(With pt.RowFields(1).PivotItems: [Characteristics].[Reason].[Reason] )
Hope it helps
Hello,
I can't see you're adressing your rowfields.
I think you got just the pivotField, but not in state as a rowfield, so maybey that's the reason why you have no count?
Maybey you could try something like:
Dim pi As PivotItemFor Each pi In pt.RowFields(1).PivotItems If pi.Visible = True Then 'do your thing Debug.Print pi.Value End IfNext
(With pt.RowFields(1).PivotItems: [Characteristics].[Reason].[Reason] )
Hope it helps
Thank You Wouter It's working now.
Pramod
Hi Wouter, Can you please take a look at this question as well: https://social.msdn.microsoft.com/Forums/en-US/f060e40e-1273-4802-a2fa-39f3742c5674/how-to-iterate-through-the-pivot-items-in-pivot-field-inside-report-filter?forum=isvvbaPramod

Related Links

OMG my project has crashed
Assigning Range of Cells to Array
Create Code to Update Value in Record From Previous Data Record
MSComm -Having problem receiving data from modem
Exporting Microsoft Word Review Comments to Microsoft Excel - Error 91 ??
error 1004 application defined or object defined error
Change File Names Using SQL Query to get new UID
Listbox question (prevent refresh)
Listview.listitem.key Intermitent Failure
varEntryIDs = Split(EntryIDCollection, ",")
Is it possible to use code in VBA to manipulate files in Windows Explorer?
VBA Error Run time Error "Browser recently got upgraded to IE9 from IE8
How to change destination project for imported modules?
Copy/Paste Columns from Sheet 1 to Sheet based on cell dropdown change on sheet 1
Delete Multiple Chart Titles in same worksheet using VBA
Open Statement: Get Count of Lines without using Open Twice?

Categories

MSDN
Windows Desktop Develo...
Windows Phone Development
Microsoft Office for D...
Windows Phone Dev Center
Commerce Server
JScript and Scripting
Microsoft Robotics
Windows Forms
Windows Live Developer
Feedback and Discussions
Development
Visio 2010
Exchange Server
Unified Communications
Archived Forums M
Microsoft Partner Center
Announcements for all ...
MSDN and TechNet Profi...
Visual Studio Code Vis...
Visual Studio Tools fo...
Publishing Windows Sto...
Payouts and Reporting
Application Security f...
Windows Filtering Plat...
Azure SQL Database
Azure Management Portal
Azure Media Services
Azure Networking (DNS,...
Azure HDInsight
Azure API Management
Azure DocumentDB
.NET Framework Setup a...
.NET Framework Class L...
Windows Presentation F...
Commerce Server 2007
ADO.NET Managed Providers
LINQ to SQL
Microsoft Live Labs Volta
Microsoft Translator H...
Visual Basic for Appli...
Microsoft Robotics - C...
Microsoft Device Emulator
Windows Mobile Develop...
.NET Framework inside ...
SQL Server Replication
SQL Server Security
SQL Server Database En...
SQL Server Migration
Standard 7: Embedded E...
Windows Embedded Compa...
SQL Server 2012 Databa...
SQL Server 2012 SQL To...
Live Connect (Archived)
Orchard –Discussions
Office XML, ODF, and B...
Microsoft Unified Comm...
Microsoft Unified Comm...
Silverlight 5
Silverlight 4
MVVM / ViewModel Patte...
Silverlight for Window...
Windows Performance To...
Customer Care Framework
.NET Framework 4 setup...
Architecture Events
Archived: Software Tes...
Building Windows Store...
Dynamic Languages on .NET
DNS and Traffic Manager
Game Technologies: Gen...
General Troubleshootin...
Installing and Registe...
LightSwitch HTML Clien...
Managed Extensibility ...
Microsoft Security Dev...
Microsoft Unified Comm...
Office Development wit...
Office Live Small Busi...
Office Live Small Busi...
Performance Counters f...
Phoenix
Oracle on Azure
Parallel Programming w...
Search Server: Develop...
Search Server: Known B...
SQL Server Modeling
SQL Server in Azure VMs
SQL Server 2014 SQL To...
Team Foundation Server...
Tools for Windows Stor...
Visual Basic Power Packs
Visual Studio Applicat...
Visual Studio Team Sys...
Windows Desktop Vista ...
Working with Data in W...
Windows Phone Preview ...
Windows Phone Preview ...
OneDrive Development (...
Kinect for Windows v2 SDK
.NET Native Preview

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile