Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Stepping Through a Non-Contiguous Range of Cells.

Stepping Through a Non-Contiguous Range of Cells

Written by Allen Wyatt (last updated August 23, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


7

Stephe needs to develop a macro that will perform an operation based on the cells that a user selects before running the macro. She knows how to do this if the user selects a range of cells, but she doesn't know how to step through the cells in a selection if the user selects a non-contiguous range of cells.

When it comes to VBA, there is very little difference between a contiguous selection and a non-contiguous selection. Excel lets you access each of them the same. Consider the following code snippet:

Dim c As Range

For Each c In Selection
    ' do something here
    MsgBox c.Address & vbTab & c.Value
Next c

In this case the cells in the selected range are stepped through, one at a time, using the For ... Next loop. Inside the loop the c variable represents an individual cell and can be used in references, as shown.

If, for some reason, you want to access each contiguous area within the selection, you can do so by specifically addressing the Areas group, as shown in this snippet:

Dim a As Range
Dim c As Range

For Each a In Selection.Areas
    'Now each a refers to a contiguous range
    'Do something here with areas, if desired
    For Each c In a.Cells
        'Now each c refers to a cell in the area
        'Do something here
        MsgBox c.Address & vbTab & c.Value
    Next c
Next a

You should also note that if the range you want to access (contiguous or non-contiguous) has been named in Excel, you can also access just the cells in the named range. Simply replace the word "Selection" in each of these examples with name of the range, in this manner:

Dim c As Range

For Each c In Range("MyNamedRange")
    ' do something here
    MsgBox c.Address & vbTab & c.Value
Next c

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8703) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Stepping Through a Non-Contiguous Range of Cells.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Getting Input from a Text File

VBA includes some commands that you can use to read information from text files (non-Word documents). These commands can ...

Discover More

Disabling a Function Key

Function keys are used to perform common tasks in Excel. If you want to disable one of the function keys, it's rather ...

Discover More

Trouble Recording Paste Special Formula

Sometimes, when you upgrade to a new version of Excel, you could run into a problem recording macros that you had no ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Replacing Commas with Periods

Want to replace all commas in a formatted number with periods, and vice-versa? There are a couple of approaches you can ...

Discover More

Understanding the Personal Workbook Filename

The personal macro workbook is a great place to save your macros that you want to use with any workbook. The personal ...

Discover More

Editing Macros

Even if you do nothing but record macros, sooner or later you will have a need to edit what you record. Here's how to get ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is three more than 9?

2019-01-22 05:03:49

Peter Atherton

Alan

Thanks for the feedback, I was definitely on the wrong track. It was good to see a post by Gary's Student again.


2019-01-21 05:44:27

Alan

Hi Peter, I found an answer on Stack Overflow, to loop through my non-contiguous selection by columns, see:

https://stackoverflow.com/questions/54219683/non-contiguous-for-each-loop-per-row-instead-of-column

It also has an image of my selection (example).

Thanks for your help.
Alan


2019-01-18 04:47:02

Peter Atherton

Alan

I remembered the shorter way before going to bed last night.

Sub ER()
Dim r1 As Range, r2 As Range, r3 As Range, rng As Range
Dim c As Range

Set c = ActiveCell
c.Resize(1 + 2, 1).Select
With Selection
Set r1 = .Offset(0, 0)
Set r2 = .Offset(0, 2)
Set r3 = .Offset(0, 4)
End With
Set rng = Application.Union(r1, r2, r3)
rng.Select
End Sub


2019-01-17 11:24:35

Peter Atherton

Alan

I'm not happy with this but it works. If the Offsets are wrong change them for address1 and addr2

Sub ExtendRange()
'suggested shortcut key Ctrl + Shift + R

Dim Col As Integer, l As Long
Dim rng As Range, j As Integer
Dim addr0 As String, addr1 As String, addr2 As String

With ActiveCell
l = .Row
Col = .Column
addr0 = Range(Cells(l, Col), Cells(l + 2, Col)).Address
End With
addr1 = Range(Cells(l, Col + 2), Cells(l + 2, Col + 2)).Address
addr2 = Range(Cells(l, Col + 4), Cells(l + 2, Col + 4)).Address
Set rng = Application.Union(Range(addr0), Range(addr1), Range(addr2))

rng.Select

End Sub


2019-01-16 04:26:19

Alan

Hi Peter, I was looking for a way to do it in VBA?

Note I also need to determine the address or at least sheet row so I can then refer to other (non-selected) cells on the same row(s).


2019-01-15 04:50:02

Peter Atherton

Alan

Select the non-contiguous range before entry, then tab or press Enter and the you move ithrough each column before going to the next.


2019-01-14 08:44:55

Alan

A For Each steps through the non-contiguous selection per row (e.g. row 1 cell (column) 1,2,3; row 2 cell 1,2,3). I'd like to cycle through the selection by columns (e.g. column 1 cell (row) 1,2,3; column 2 row 1,2,3 etc.).

What is the easiest way to do this?

I don't necessarily need code, just an explanation and I'll be able to code it myself...

(I also need to access the address (in part) of each in order to refer to other cells on the same row).

Regards
Alan


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.