
- #Create pivot tables in excel 2010 how to
- #Create pivot tables in excel 2010 code
- #Create pivot tables in excel 2010 professional
To delete a pivot table, you need to use the PivotTables class. To display it again, we add it back by using the AddPivotFields procedure we covered earlier. Hiding a field is a matter of setting its Orientation to xlHidden. Private Sub DisplayField(pivotTable As Excel.PivotTable, _ Re-displaying is too as long you understand that after you hide a field, you need to add it back to display it. As a special touch, I apply a NumberFormat to display the column as a percentage. This is different from the previous examples that called AddDataField. Notice this field is added to the CalculatedFields collection. In the comment, I show a good example of a formula. PivotTable.PivotFields(fieldName).NumberFormat = "0.00%" End Sub PivotTable.PivotFields(fieldName).Orientation = _Į PivotTable.CalculatedFields().Add(fieldName, formulaText, True) 'formulaText="= ('Shipping Fee'+Taxes )/'Order Total' The main problem is if the user refreshes the pivot table, their column disappears! WTW! The proper way to do this is use the Add calculated field feature to build a formula and insert it as a pivot table field. I’ve seen users create pivot tables and then add their own calculated field to it. This method accepts a few self-explanatory parameters (if you disagree, just use the comments to ask a question) and adds a field to the passed pivot table.Ĭalculate fields are very cool. PivotTable.AddDataField(pivotTable.PivotFields(fieldName), fieldCaption, fieldType)
#Create pivot tables in excel 2010 how to
Sure, I just showed you how to do it, but what if you wanted a reusable function that did it for you? Private Sub AddPivotFields( _įieldType As Excel.XlConsolidationFunction) If you have a pivot table, you might want to automate the addition of a field. We then tell the wizard the data source type, the data source name, and where to place the pivot table.Īfter creating the pivot table, the method adds a row field and two data fields (a count and sum of the Order Total column). Then, it creates a new pivot table with the help of the PivotTableWizard. The procedure first creates a new worksheet that will serve as the location for the new pivot table. ' ) 'AddPivotFields(pt, "Order Total", "Total For Date", _ '-OR- 'AddPivotFields(pt, "Order Total", "Order Count", _ Pt.AddDataField(pt.PivotFields( "Order Total"), "Total for Date", _ Pt.AddDataField(pt.PivotFields( "Order Total"), "Order Count", _
#Create pivot tables in excel 2010 professional
'To be professional or merely resuable, the name could be passed as parameter With pt.PivotFields( "Order Date") TargetSheet.PivotTableWizard(, _ĭim pt As Excel.PivotTable = targetSheet.PivotTables(1) Private Sub CreatePivotTable(tableName As String)ĭim targetSheet As Excel.Worksheet = ĭim ptName As String = "MyPivotTable" 'We'll assume the passed table name exists in the ActiveWorkbook I prefer to work with tables as they are cleaner and more structured (as compared to ranges). In this sample, I pass a table to use as the basis of the pivot table. To create an Excel PivotTable you need a range filled with data or a table.

I then used this data as the basis of my pivot table. NOTE: I imported data from the OrderSummary query of the Northwind sample Access database.
#Create pivot tables in excel 2010 code
This code will help you relate to them and make their life easier. It’s quite likely your user base is 100% comprised of uber-serious spreadsheet jockeys. Today, I’ll show-off some code that shows how to automate pivot tables. They made my life easier and impressed my clients and my boss’s boss (but not my boss… he knew me too well). When I was a serious spreadsheet jockey trying to make my mark on the world (as a financial auditor for a super-serious accounting firm), I used pivot tables all the time… whether I needed to or not. Long before Big Data was even a twinkle in its mother's eye, there was Excel and its pivot tables. Microsoft Excel is the original business intelligence tool.
