Infinite opportunities?  Endless problems?  Limited resources?  Talk to us!
Operations
Consulting
Software
Consulting
Publishing and
Training
Applied
Solutions
Excel add-ins
and tutorials
Charts Excel and VBA
Case Studies
VBA
tutorials
PowerPoint
Add-Ins
Miscellenia
You are on the Home/Publications & Training/Case Studies/Index into a multi-area range page

“Indexing” into a multi-area range

Most, if not all, developers who work with Microsoft Excel have had reason to “pick” a particular cell in a range with syntax like

myRange.Cells(1)

A problem arises with the above when myRange contains multiple areas (i.e., it is not a single contiguous range) because the Cells property assumes a single-area range.  Try the following quick test.  In Excel, select cells A1, A5, and A8:A9 (to do so select A1, then hold the CTRL key down and select A5, then while still holding CTRL down, click and drag to select A8:A9).  In the Visual Basic Editor, open the Immediate Window and enter

?selection.cells.count

The answer will be 4.

Now, enter

?selection.cells(2).address

The answer will be $A$2!  Of course, we would like it to be $A$5.

A quick workaround

The function below returns the expected cell.  Further, and unlike the Cells property, if the index is greater than the number of cells in the range, it returns nothing.  The code below goes in a standard module

Option Explicit

 

Function RangeIndex(aRng As Range, ByVal Idx As Long) As Range

    'Returns the cell at aRng(Idx) taking into account _

     aRng containing multiple areas, whereas Cells(Idx) _

     assumes there is only one area. _

     The result of this function also differs from the _

     Cells(Idx) property in that if Idx > total number of _

     cells in aRng, this function returns nothing.

   

    If Idx > aRng.Cells.Count Then Exit Function

   

    Dim AreaIdx As Long

    AreaIdx = 1

    Do While aRng.Areas(AreaIdx).Cells.Count < Idx

        Idx = Idx - aRng.Areas(AreaIdx).Cells.Count

        AreaIdx = AreaIdx + 1

        Loop

    If Idx >= 1 Then Set RangeIndex = aRng.Areas(AreaIdx).Cells(Idx)

    End Function

 

About this site

What's new

Google
Web
This Site
 

 

You can test the function with

?rangeindex(selection,2).Address

The result will, as expected, be $A$5 and

?rangeindex(selection,3).Address

will return $A$8.

Summary

This tip provides a more reliable mechanism to identify the cell at a particular index in a multi-area range.

 

For custom technology solutions, operations consulting, or training contact web-underscore-contact@tushar-hyphen-mehta-dot-cee-oh-em.
By accessing any page or link on this web site other than this page, you agree to the terms and conditions.

Ads from amazon.com

[Optional] Survey (current rating of site: 3.7 out of 4)  If you will take a moment to provide your comments, it will help improve the site both for you, and for other visitors.

On a scale of 4 (just what I need)
to zero (totally useless)

How do you rate the information

on this page?

  on this site?

[Optional]

Your name
Your email address
Other comments
 
Monitor page
for changes
    
   it's private  

by ChangeDetection
A comment selected at random:

 

 

Copyright © 2000-2008 Tushar Mehta.
Send comments and suggestions about the web site to webmaster@tushar-hyphen-mehta-dot-cee-oh-em
Last edited April 14, 2008