Giter Site home page Giter Site logo

pivotwrap's Introduction

PivotWrap

Pivot Table Wrapper Class for Excel VBA

About

The pivot table is arguably Excel's most powerful tool. It can summarize so much data in so little time with no more than a few clicks and drags. But creating and controlling one using VBA is a different story. The amount of boilerplate required to build the simplest of pivot tables is mind-bloggling. If you're an Excel power user who enjoy writing code from scratch to interact with pivot tables in your VBA projects, then you're probably the only one out there, end if.

PivotWrap is a set of VBA class modules that streamlines the pivot table coding experience for greater productivity. It takes care of the boilerplates for you so you can focus on building pivot tables of all complexities using concise, intuitive class methods.

Installing PivotWrap

Open up the Visual Basic Editor (Alt+F11) in your macro-enabled project workbook, then import (Ctrl+M) both clsPt.cls and clsPtField.cls files. Once imported, you'll see them in the Class Modules folder in the Project Explorer (Ctrl+R).

You'll also need to enable the Microsoft Scripting Runtime reference. Go to Tools/References and set a tick the checkbox beside Microsoft Scripting Runtime. This reference enables the use of the Scripting library and the Scripting.Dictionary class that is used in PivotWrap.

Using PivotWrap

Initializing Class Module

First, select the Range of the source data.

Dim rng as Range
Set rng = ActiveWorkbook.Worksheets("SourceData").Range("A3:J199")

Make sure that the top row of the Range is the header row. As best practice, each header should be unique. Note that Excel allows you to create a pivot table with duplicate headers in the source data (It adds a number suffix to one of them in the PivotTable Fields but the source data headers remain duplicated). In contrast, PivotWrap will not allow duplicate headers in the source data. It will directly overwrite the offending header in the source data with an added suffix.

Once the source data Range is defined, PivotWrap can be initialized.

Dim pt As PtW
Set pt = New PtW

pt.init rng

The init method accepts a Range argument, which it uses as the SourceData for creating a pivot cache.

Adding Pivot Fields

Pivot fields can be added individually or collectively in an array. Adding pivot fields one by one offers more control over the field settings.

Dim ptf As PtWField

' Create a "Region" PivotWrap row field, 
' sort by "Revenue" in descending order, 
' filter by top 12 items in "Revenue"
Set ptf = pt.add_row_field( _
	field_name:="Region", _
	position:=1, _
	sort_by:="Revenue", _
	sort_order:=xlDescending, _
	filter_type:=xlTopCount, _
	filter_by_field_name:="Revenue", _
	filter_value1="12")

' Create a "Month" PivotWrap column field
pt.add_column_field "Month", 1

' Create a "Year" PivotWrap page field,
' set current page to 2012, 2016, 2017
pt.add_page_field "Year", 1, , Array("2012", "2016", "2017")

' Create a "TotalRevenue" PivotWrap data field
Set ptf = pt.add_data_field("TotalRevenue", "Revenue", 1, , xlSum)

' Create an "AverageRevenue" PivotWrap data field
pt.add_data_field "AvgRevenue", "Revenue", 2, , xlAverage

Creating multiple PtWFields with the same orientation is straightforward

Dim ptfs() As ptWField

Set ptfs = pt.add_row_fields(array("Region", "Year"))

pt.add_column_fields array("Month", "Weekday")

Creating Pivot Table

Once pt has been populated with pivot fields, call pt.create to create a pivot table. pt.create has several optional parameters to choose output range and override output fields.

' Creates a pivot table at Range("C10") of Worksheet "Output" in ActiveWorkbook
pt.create destination_row:=10, destination_column:=3, _
	table_name:="MyPivotTable", _
	destination_worksheet_name:="Output", _
	destination_workbook:=ActiveWorkbook


' Default behaviour: Creates a pivot table at Range("C3") of Worksheet "PivotTable" in ActiveWorkbook
pt.create

pivotwrap's People

Contributors

paunchymochi avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.