Written by Allen Wyatt (last updated August 23, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
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.
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!
Want to replace all commas in a formatted number with periods, and vice-versa? There are a couple of approaches you can ...
Discover MoreThe personal macro workbook is a great place to save your macros that you want to use with any workbook. The personal ...
Discover MoreEven 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 MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments