Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the Periods argument of Excel.Range.Group take an array?

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.

like image 390
Drew Chapin Avatar asked Sep 25 '13 17:09

Drew Chapin


1 Answers

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.

like image 165
Siddharth Rout Avatar answered Oct 20 '22 10:10

Siddharth Rout