Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


March 14, 2005

Produce Pivot Tables Programmatically

A new spin on an old favorite
RSS
Subscribe to Windows IT Pro | See More Task Automation Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

A Look at PivotTable2.vbs
PivotTable2.vbs creates a worksheet called Test and populates it with a small list of user account data under the headers UserID, Account Locked, and Disabled Account. Listing 2 shows an excerpt from PivotTable2.vbs. You can download the entire script (as well as PivotTable.vbs) from the Windows Scripting Solutions Web site. Go to http://www.windowsitpro.com/windowsscripting, enter 45502 in the InstantDoc ID text box, then click the 45502.zip hotlink.

PivotTable2.vbs is similar to PivotTable.vbs, except that PivotTable2.vbs programmatically resolves the value for the lastcell variable and filters the test data so that the pivot table displays only specific values (i.e., locked accounts). First, PivotTable2.vbs populates the spreadsheet with the test data. Next, the script programmatically obtains the value for the lastcell variable, as callout A in Listing 2 shows. Now if you add new headers, you don't have to manually go in and change the range. The code at callout A uses the SpecialCells method with the value of 11, which represents the xlLastCell constant. The Address property returns the cell row and column. The xllastcell variable stores this information, which the script uses in the PivotTableWizard method call, which callout B in Listing 2 shows.

The code at callout C in Listing 2 illustrates how to use the Subtotals property to suppress subtotals in a pivot table. This property has 12 elements, which Table 2 lists. Setting an element to True indicates you want a subtotal for that element; setting an element to False means you don't want a subtotal for that element. You always get at least one subtotal (i.e., a count of records) in a pivot table, even if you suppress all the subtotal elements. In PivotTable2.vbs, I suppressed all the elements and used the unsuppressable subtotal for the Grand Total row count of all the locked accounts.

The last key task that PivotTable2.vbs performs is displaying the locked accounts. When I first worked on scripting this task, I thought that I'd just have to specify the values I wanted to see. However, contrary to the way I thought it would work, I discovered I had to filter out every value in a specific field that I didn't want to appear by setting its Visible property to False. For this example, there are only two possible values for the Account Locked status: Yes (i.e., the account is locked) and No (i.e., the account isn't locked). So, I set the Visible property to False for those data fields that have the value of No in the Account Locked column, as the code at callout D in Listing 2 shows. In this code, PivotTables(xldata) refers to the source worksheet (i.e., the Test worksheet. PivotFields("Account Locked") refers to the column with the Account Locked header. And PivotItems("No") selects those data fields that have the value of No under this header; the code excludes these data fields by setting the Visible property value to False. Therefore, only locked-out accounts will be appear in the pivot table.

Using and Adapting the Scripts
To use PivotTable.vbs and PivotTable2.vbs, you need to be running Windows 2000 or later and Microsoft Office 2000 or later. After you run and experiment with these scripts, you can adapt them to create pivot tables that display data from your organization. To help you find the methods and properties you need, you can use the resources mentioned in the sidebar "How to Learn About Excel's Methods and Properties."



HOW TO LEARN ABOUT EXCEL'S METHODS AND PROPERTIES
Microsoft Excel's macro recorder is an excellent resource for learning about how to write Excel-specific code that you want to incorporate into VBScript scripts. To use the macro recorder for this purpose, you simply need to record a macro while you perform the actions you want your script to perform, then open that macro for editing. In most cases, when you look at the recorded macro, you'll find the methods and properties that you need to use.

You can use the Object Browser or the Immediate pane in Excel's Visual Basic Editor to obtain the values for constants, such as xlLastCell or xlMinimized. To access the Object Browser and Immediate pane, bring up Visual Basic Editor by pressing Alt+F11, then press F2 to get the Object Browser and Ctrl+G to get the Immediate pane. Let's say that you want to find the value for the xlLastCell constant. To use the Object Browser, enter xlLastCell in the text box next to the binocular icon, then click that icon. To use the Immediate pane, type

MsgBox xlLastCell
in the pane, then press Enter. In both cases, the value of 11 appears.


End of Article

   Previous  1  [2]  Next  


Reader Comments
good

downbylhl August 28, 2006 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Windows 7 Sets Sales Record

Microsoft CEO Steve Ballmer described Windows 7's first ten days of sales as "fantastic" while in Japan yesterday. ...


Task Automation Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Will Your Next Generation Server System Meet Your Infrastructure Optimization Needs?

Continuous Data Protection and Recovery for Microsoft Exchange

Related Events Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement