Dynamic range

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I would like to set a range based on a series of rows.

For example, the series might be: 1,2,3,6,7,8,11,12,13, ..... , 21,22,23

I don't want to write it all out, ie:

Code:
Dim rng As Range
Set rng = Range("A1:A3, A6:A8, A11:A13, A16:A18, A21:A23")

because my series could be very long.

Is there a way to set a range by incorporating a For Next Loop, for example:

Code:
' pseudocode

Dim rng As Range

Dim i As Integer, j As Integer

For i = 1 To 21 Step 5

    For j = i To i+2

        Set rng = rng & "A" & j

    Next j

Next i

Thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Something like:

Code:
For i = 1 To 21 Step 5

   If rng is nothing then
      Set rng = Cells(i, "A").Resize(3)
   else
      Set rng = Union(rng,  Cells(i, "A").Resize(3))
   End If

Next i
 
Upvote 0
Solution
Something like:

Code:
For i = 1 To 21 Step 5

   If rng is nothing then
      Set rng = Cells(i, "A").Resize(3)
   else
      Set rng = Union(rng,  Cells(i, "A").Resize(3))
   End If

Next i
Brilliant, thanks a lot.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,387
Members
449,080
Latest member
Armadillos

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