How to access Nth cell in non-contiguous range?

NigelTufnel

Board Regular
Joined
Apr 3, 2008
Messages
53
Office Version
  1. 365
Platform
  1. Windows
Suppose I have a range consisting of not-necessarily contiguous cells in the same columnn, e.g.,

Set rMyRange = Range("$A$1:$A$3, $A$8, $A$10")

How do I directly access the Nth cell in the range?

For example, I tried rMyRange.cells(4).address...but I get $A$4 instead of $A$8, which is what I want.

I was able to pick off the fourth item using a For loop (e.g.,

For each rMyCell in rMyRange

But this is tedious. Is there a way to directly read the fourth cell?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
No. Most properties, like .Item and .Cells, act as if applied to the top left cell of a range, discontinous or otherwise.

A loop is needed. In a general case, one might loop through Areas rather than Cells

Code:
Function NthCell(someRange As Range, cellSought As Long) As Range
    Dim cCount As Long
    Dim oneArea As Range
    
    For Each oneArea In someRange.Areas
        If oneArea.Cells.Count < cellSought - cCount Then
            cCount = cCount + oneArea.Cells.Count
        Else
            Set NthCell = oneArea.Item(cellSought - cCount)
            Exit Function
        End If
    Next oneArea
End Function
 
Upvote 0
Mike--

Thanks for the response. It's funny how every once in awhile you find something in VBA that seems like it should be trivial, but actually requires some coding.

And thanks for supplying the code!
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top