Recently I was learning how to automate the creation of a pivot table in Excel with VBA, and the implementation of the Excel.Range.Group()
method struck me as odd. The fourth parameter, Periods
, takes a 7 element array of Boolean values to indicate whether the grouping is by Seconds, Minutes, Hours, Days, Months, Quarters, or Years. Normally, you would see something like this done by using an Enum
type whose members can be Or
'd together to signify that one or more of the options is turned on - The MsgBox
function is a good example of that.
For example, I would have expected the usage to be more like this...
MyPivotTable.DataRange.Cells(1).Group Periods:=vbGroupPeriods.Days Or _
vbGroupPeriods.Months
Instead of...
MyPivotTable.DataRange.Cells(1).Group Periods:=Array(False, False, False, _
True, True, False, False)
I've looked around to try and understand why it's done with an Array
, but have thus-far come up empty handed. So, my question is why was it done this way? Is there some limitation that I'm not seeing? Was it somebody's personal choice? Or is it just a mystery? Not trying to complain, just trying to understand.
Q1. why was it done this way?
Maybe because it gives you more flexibility? For me it is easier to type True
7 times
Array(True, True, True, True, True, True, True)
rather than saying
vbGroupPeriods.Seconds Or _
vbGroupPeriods.Minutes Or _
vbGroupPeriods.Hours Or _
vbGroupPeriods.Days Or _
vbGroupPeriods.Months Or _
vbGroupPeriods.Quarters Or _
vbGroupPeriods.Years
Also the sequence in the array is not very difficult to remember...
Seconds-->Minutes-->Hours-->Days-->Months-->Quarters-->Years
Q2. Is there some limitation that I'm not seeing?
No There is no limitation as such.
Q3. Was it somebody's personal choice? Or is it just a mystery?
Nope :) No Mystery. It is never someone's personal choice. There is a team specifically for Excel Development at Microsoft which decides and inculcates a specific feature/functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With