Creating Dynamic Ranges With A Macro: Why Use The Macro?
Creating Dynamic Ranges With A Macro: Why Use The Macro?
Creating Dynamic Ranges With A Macro: Why Use The Macro?
The macro in this workbook is designed to automatically create Dynamic Ranges in a table of data.
There is a button on the Data sheet that runs the macro.
Why Use the Macro?
If you choose Insert>Name>Create, and selecting the option Top Row, you can get Excel to
automatically set up named ranges for each column heading.
This is quick and easy, but the ranges created are static, and will not grow as more lines of data are
added.
Spaces in the header title are substituted with underscores, as Names cannot contain spaces.
Note: If you have very long descriptive names in each header, then you could insert a new row above
the header, into which shorter but relevant names can be created. This row could then be hidden. If
you'd still like to have column identifiers rather than descriptive names then you could set up in a
hidden row as, ColA, ColB, ColC etc.
How Does the Macro Work?
Four constants are set in the macro
Rowno - the row containing Headings, which would normally be 1
ROffset - the number of rows below Rowno that the actual data starts. In this example it is 1
Colno - the first column containing data, again normally column 1
COffset - the number of columns to the right of Colno which will always contain data. In this
example it is 0
The code used to generate these Dynamic Ranges can be seen by pressing Alt + F11 to invoke the
Visual Basic Editor.
Then look at the code inside module Technology4U
el to
of data are
g. INDEX is
aces.
w row above
hidden. If
up in a
it is 1
n this
nvoke the
YearMth Region Employee Item Units Unit Cost Total
dition to the Names for the column ranges, 3 extra names are
ed
w for the last row number used on the sheet
l for the last column number used on the sheet
Data for the complete range of Data including the Header row.
Dynamic Range has been created using the Index function, rather
the volatile Offset Function.
as opposed to
=SUMPRODUCT(--($B$2:$B$50=$C8),$G$2:$G$50)
or to write
=SUMPRODUCT(--(Region=$C20),--(Item=D$19),Total)
as opposed to
=SUMPRODUCT(--($B$2:$B$50=$C8),--($D$2:$D$50=D$20),$G$2:$G$50)
The formulae above use the double unaray minus construct -- to coerce the results of the tests
from True to 1 and False to 0 so that they can be utilised in the Sunmproduct function.
They could equally have been written with the "*" construct which would carry out the coercion.
=SUMPRODUCT((Region=$C20)*(Item=D$19)*Total)
TOTAL
3,325
23,785
5,816
YearMth (All)
The Pivot table report shown above has been based upon the Dynamic Range
myData
which was created automatically with the macro CreateNames.
The formulae in cells O9:T11 are Sumproduct formulae but they have been array entered us
Control + Shift + Enter. (see also, final note on better alternative method)
{=SUMPRODUCT((Region=$N9)*(Item=O$8)*IF($O$6="All",1,(YearMth=$O$6))*Total)}}
It is necessary to use an array entry of Sumproduct (which itself is an array formula) becau
of the introduction of the IF statement within it.
Because we need to show the result for All dates, then if cell D6 contains "All", the IF formu
uses 1 (or True) in the Sumproduct formula rather then returning 1 or 0 (True or False) for t
other comparisons against whether the dates match the value in D6.
This report emulates the report shown Pivot Table Report on the left.
It has been designed to try and emulate the Pivot Table, but can't produce as much flexibili
and does require writing individual formulae.
I am grateful to David Patton for pointing out that there are alternatives to "array entering"
Sumproduct formulae above, using the OR construct i.e. inserting a "+" condition.
=SUMPRODUCT(--(Region=$N9)*(Item=O$8)*((YearMth=$O$6)+($O$6="All"))*(Total))
This is a far better method, and is to be preferred over the array entering method as describ
above.
ynamic named ranges
TOTAL
3,325
23,785
5,816
32,925
earMth=$O$6))*Total)}}
he left.
($O$6="All"))*(Total))
Contextures Recommends