Anda di halaman 1dari 11

Creating Dynamic Ranges With A Macro

by Roger Govier -- roger@technology4u.co.uk

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.

What Does the Macro Do?


This macro uses the INDEX function to create a dynamic range for each column heading. INDEX is
used instead of OFFSET, which is a volatile function, and can slow down the workbook.

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

Created by Roger Govier


email ; roger@technology4u.co.uk
ble of data.

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

2006-12 Quebec Jones Pen Set 700 1.99 1,393


2006-12 Ontario Kivell Binder 85 19.99 1,699 Pressing the button "Create Dynam
2006-12 Ontario Howard Pen Set 62 4.99 309 names for each of the column head
2006-12 Ontario Gill Pen 58 19.99 1,159
2006-12 Quebec Anderson Binder 10 4.99 50 In addition to the Names for the co
2006-12 Quebec Anderson Pen Set 19 2.99 57 defined
2007-01 Ontario Anderson Pen Set 6 1.99 12 lrow for the last row number used
2007-01 Ontario Howard Pen Set 10 4.99 50 lcol for the last column number u
2007-01 Quebec May Paper 39 1.99 78 myData for the complete range o
2007-01 Quebec May Binder 1 8.99 9
2007-01 Ontario May Pen Set 80 4.99 399 myData can be used as the source
2007-01 Quebec May Binder 51 1.99 101
The Dynamic Range has been crea
2007-01 Quebec May Binder 10 19.99 200
than the volatile Offset Function.
2007-01 Quebec May Pen Set 15 4.99 75
2007-01 Ontario May Desk 31 125.00 3,875 Having determined Lastrow (lrow)
2007-01 Quebec Jones Pen Set 46 15.99 736 =COUNTA($A:$A)
2007-02 Ontario Kivell Binder 61 8.99 548
2007-02 Quebec Jones Pen 90 8.99 809 named ranges like Region are det
2007-02 Quebec Parent Pen 43 19.99 860 =$B$2:INDEX($B:$B,lrow)
2007-02 Ontario Kivell Pen Set 32 4.99 160
2007-02 Ontario Smith Pen Set 37 1.29 48 The code looks at each name in the
2007-02 Quebec Parent Pen Set 26 15.99 416 underscore where there are any sp
2007-02 Ontario Gill Binder 79 8.99 710 Unit_Cost as Names cannot conat
2007-02 Ontario Smith Pen Set 72 15.00 1,080
2007-02 Quebec Jones Pen Set 27 4.99 135 The code used to generate these D
2007-02 Quebec Jones Binder 5 19.99 100 pressing Alt + F11 to invoke the Vis
Then look at the code inside modu
2007-03 Ontario Howard Pen Set 59 4.99 294
2007-03 Ontario Gill Pen Set 41 1.99 82
2007-03 Quebec Howard Pen 85 4.99 424
2007-03 Ontario Gill Pen Set 61 1.29 79
2007-03 Ontario Gill Binder 9 8.99 81
2007-03 Ontario Kivell Desk 52 125.00 6,500
2007-03 Quebec Jones Pen Set 75 4.99 374 There are examples of using the rang
2007-03 Ontario Kivell Pen Set 97 12.49 1,212 sheet Example Sumproduct formulae,
2007-03 Ontario Kivell Pen Set 86 23.95 2,060 Tables on sheet Example Pivot Table
2007-03 Alberta Smith Desk 8 275.00 2,200
2007-04 Ontario Gill Pen Set 90 1.29 116
2007-04 Alberta Smith Pen 33 1.99 66
2007-04 Alberta Smith Binder 53 19.99 1,059
2007-04 Ontario Gill Pen Set 25 1.29 32
2007-04 Ontario Howard Binder 87 4.99 434
2007-04 Ontario Howard Binder 95 19.99 1,899
2007-04 Ontario Gill Binder 68 4.99 339
2007-04 Ontario Gill Binder 19 4.99 95
2007-04 Ontario Gill File Folder 19 5.99 114
2007-04 Ontario Gill File Folder 19 5.99 114
2007-05 Ontario Gill Binder 19 4.99 95
2007-05 Ontario Gill Binder 19 4.99 95
2007-05 Ontario Gill Binder 19 4.99 95
sing the button "Create Dynamic Range Names" creates a series of
es for each of the column headings on the sheet.

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.

ata can be used as the source range for a Pivot Table

Dynamic Range has been created using the Index function, rather
the volatile Offset Function.

ng determined Lastrow (lrow) with the formula


UNTA($A:$A)

ed ranges like Region are determined with the formula


$2:INDEX($B:$B,lrow)

code looks at each name in the header row, and substitutes an


rscore where there are any spaces e.g. Unit Cost becomes
_Cost as Names cannot conatin spaces.

code used to generate these Dynamic Ranges can be seen by


sing Alt + F11 to invoke the Visual Basic Editor.
look at the code inside module Technology4U

e are examples of using the range Names in Sumproduct formulae on


Example Sumproduct formulae, and examples using myData with Pivot
s on sheet Example Pivot Table Report
How much easier and instructive it is to write
=SUMPRODUCT(--(Region=$C8),Total)

Region Units Total


Alberta 94 3325.14
Ontario 1497 23784.54
Quebec 1242 5815.58

as opposed to
=SUMPRODUCT(--($B$2:$B$50=$C8),$G$2:$G$50)

or to write
=SUMPRODUCT(--(Region=$C20),--(Item=D$19),Total)

Region Binder Desk File Folder Paper Pen Pen set


Alberta 1,059 2,200 0 0 66 0
Ontario 6,090 10,375 228 0 1,159 5,932
Quebec 460 0 0 78 2,093 3,185

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

results of the tests

y out the coercion.


Pivot Table report based upon dynamic range myData

YearMth (All)

Sum of Total Item


Region Binder Desk File Folder Paper Pen Pen Set Grand Total
Alberta 1,059 2,200 66 3,325
Ontario 6,090 10,375 228 1,159 5,932 23,785
Quebec 460 78 2,093 3,185 5,816
Grand Total 7,610 12,575 228 78 3,318 9,117 32,925

The Pivot table report shown above has been based upon the Dynamic Range
myData
which was created automatically with the macro CreateNames.

Pivot tables provide the ultimate in flexibility in filtering and


re-arranging your reports and do not involve the writing of any formulae in order to
be created.
Similar report created with Sumproduct and dynamic named ranges
Cell O6 has Data Validation
applied using List = YrMthList
YrMthList
All
2006-12 YearMth All
2007-01 Item
2007-02 Region Binder Desk File Folder Paper Pen Pen set
2007-03 Alberta 1,059 2,200 0 0 66 0
2007-04 Ontario 6,090 10,375 228 0 1,159 5,932
2007-05 Quebec 460 0 0 78 2,093 3,185

TOTAL 7,610 12,575 228 78 3,318 9,117

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

y have been array entered using


ve method)

earMth=$O$6))*Total)}}

elf is an array formula) because

6 contains "All", the IF formula


ng 1 or 0 (True or False) for the
in D6.

he left.

n't produce as much flexibility

ernatives to "array entering" the


ting a "+" condition.

($O$6="All"))*(Total))

y entering method as described


Contextures Products

PivotPower Premium Add-in


UserForms for Data Entry ebook Kit
Contextures Excel Tools Add-in
Data Validation Multi-Select Premium Kit
30 Excel Functions in 30 Days eBook Kit

Contextures Recommends

Jon Peltier's Excel Chart Utilities


ExcelUser Dashboards
Xtreme Pivot Table Course
Power Pivot University
Excel Courses Online
Chandoo's Excel VBA School
Chandoo's Project Management Templates

Contextures Sites & News

Contextures Excel Newsletter


Contextures Excel Tips Website
Contextures Excel Blog
Excel Pivot Tables Blog
Debra's Pivot Table Books on Amazon
Time-saving tools for pivot table power users
Step by step instructions and videos
Make instant backups, sort sheets, and many more tools
Select multiple items from a listbox, to enter in a single cell
Sample workbook and easy to follow user guide for key Excel functions

Create complex charts with just a few clicks


Templates and instructions for impressive dashboards
Video training from beginner to advanced topics
Online course by Power Pivot expert Rob Collie
Excel training by Mynda Treacy
Become awesome in Excel VBA with Chandoo's online training
Save time with Chandoo's templates for project management

Weekly Excel tips, tutorials, videos, and news


Hundreds of tutorials, tips and sample files
Read the Excel tutorials and share your comments
Pivot table tutorials and tips, with comments and questions
Pivot table books for Excel 2007