Documente Academic
Documente Profesional
Documente Cultură
Beyond Excel
BASICS
PIVOTCHART DRILLDOWN
Page Title
Beyond Excel
PivotChart Drilldown
Beyond Excel
Table of Contents
Why Read This? .......................................................................................................................................................................................... 4
Overview .................................................................................................................................................................................................... 4
Coding Chart Events ................................................................................................................................................................................... 5
Coding the Double Click Event ................................................................................................................................................................... 6
Coding the MouseUp Event ....................................................................................................................................................................... 7
Housekeeping ............................................................................................................................................................................................ 8
References and Links ................................................................................................................................................................................. 8
New to VBA?
Click Intro to VBA for instructions on how to get started coding with Visual Basic for Applications.
PivotChart Drilldown
Beyond Excel
Overview
Thanks to YouTube we can see how this
works. Click the picture at right or this link
for a short video:
www.youtube.com/watch?v=-Uu2WqDxLdk
Figure 1 PivotChart Drilldown Demo
Page 4
PivotChart Drilldown
Beyond Excel
NOTE!
Code is attached to Chart
Sheets code module
Page 5
PivotChart Drilldown
Beyond Excel
'
'
'
'
Inputs:
ElementID
Arg1
Arg2
Cancel
'
'
Notes:
'
Example:
'
'
Date
Init Modification
02/02/11 CWH Initial Programming
'
Declarations
'
'
ErrHandler:
Page 6
PivotChart Drilldown
Beyond Excel
'
'
'
'
Parameters: Button
Shift
x
y
'
Example:
'
'
'
Date
Init Modification
10/04/10 CWH Initial Programming w/some help from
http://www.computorcompanion.com/LPMArticle.asp?ID=221
'
Declarations
Dim ElementID
Dim Arg1
Dim Arg2
Mouse
State
Mouse
Mouse
As Long
As Long
As Long
'
'
'
ErrHandler:
Declaration Block:
Before writing executable code I
declare all variables. This routine has
none.
Error Handling:
Every routine should have error
handling.
End Sub
Procedure:
This routine:
Calls GetChartElement passing to it
the mouse pointers x and y
coordinates.
GetChartElement
returns what was clicked.
Checks if a series was clicked. If it
Page 7
PivotChart Drilldown
Beyond Excel
Housekeeping
Both of these methods create a new
worksheet for each drilldown request.
That can really clutter our workbooks.
To maintain good housekeeping we can
use a designated worksheet for drilldown
requests using the code at right.
The highlighted sections should be
added to the chart BeforeDoubleClick or
MouseUp event (depending on which
you use).
Declarations:
Well need a worksheet variable and
a flag to tell us if the Drilldown
worksheet exists.
Procedure:
The added lines:
Cycle through all worksheets to see if
any of them are named Drilldown
(I normally use a routine from my
library called WorksheetExists to do
this. You may want to create your
own).
If worksheet Drilldown does not
exists then rename the newly
created worksheet Drilldown.
If it does exists: Clear it; Copy the
newly created worksheet to it;
Activate it; and delete the newly
created worksheet.
'
Declarations
Dim oWks
As Worksheet
Dim bExists As Boolean
'
'
msdn.microsoft.com/en-us/library/aa195740(v=office.11).aspx
www.computorcompanion.com/LPMArticle.asp?ID=221
www.youtube.com/watch?v=-Uu2WqDxLdk
http://youtu.be/J5kPny4LaqE
Page 8