How To: Refer to first cell in a range

largeselection

Active Member
Joined
Aug 4, 2008
Messages
358
Hi,

So with some help from users on this board I have put together some code which will define a bunch of ranges for me. I need to reference the first, second, third, etc cells in the ranges I've defined now. How can I do that?


As an example: The code might run through the data and say Range1 = Cells(2,3), Cells(5,3), Cells(15,3) and Cells(25,3).

So now I need to be able to refer to first cell in Range1.offset(0,3) in another part of my code, then continue on to say second cell in Range1.offset(0,3) etc.

In my example, that would mean that I would want the code to be able to refer to:

Range1 first = Cells(2,3).offset(0,3)
Range1 Second = Cells(5,3).offset(0,3)
Range1 third = Cells(15,3).offset(0,3)
Range1 fourth = Cells(25,3).offset(0,3)

This is basically so I can pull the value of another cell a few columns over from where the data resides where the range was created.
 
Code:
Sub t()
  Dim Range1 As Range
  Dim r As Range, i As Long, a() As Variant
  
  Cells(2, 3).Value = 23
  Cells(5, 3).Value = 53
  Cells(15, 3).Value = 153
  Cells(25, 3).Value = 253
  
  Set Range1 = Union(Cells(2, 3), Cells(5, 3), Cells(15, 3), Cells(25, 3))
  ReDim a(1 To Range1.Count) As Variant
    For Each r In Range1
      i = i + 1
      a(i) = r.Address
  Next r
  
  MsgBox Range(a(1)).Value, , "First cell value in Range1"
  MsgBox a(1), , "Cell address for first cell in Range1"
  
  MsgBox Range(a(3)).Value, , "Third cell value in Range1"
  MsgBox a(3), , "Cell address for third cell in Range1"  
End Sub
 
Upvote 0

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