changing range in a loop

gordsky

Well-known Member
Joined
Jun 2, 2016
Messages
556
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have a number of ranges but for the purpose of this question will limit to 3. I have a for loop that does some things with Rng1 which I would like to repeat over the remaining ranges. I dont want to join the ranges or use a union due to the code later on in the macro. Is there a way to increment the Rng by 1 on each loop something along the lines of

Set Rng1 = Sheets(Sheet1).Range("E11:E43")
Set Rng2 = Sheets(Sheet1).Range("E55:E83")
Set Rng3 = Sheets(Sheet1).Range("K11:K43")

for i = 1 to 3

with Rng(i)

'do something

end with
next i

any help or suggestions would be appreciated
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi and thanks for the reply.
I did try that earlier but couldn't get it to work. As a test I just tried colouring the cells in each range but kept getting either object required error or out of script. Could you maybe expand on your suggestion
 
Upvote 0
Could you copy your code over I will test it out? I was will see if it works I did not test the above but if you post a copy of the code I can test It out on my end
 
Upvote 0
Sub test()
Dim Rng1 As Range, Rng2 As Range, Rng3 As Range
Dim i As Long

Set Rng1 = Sheets(Sheet1).Range("E11:E43")
Set Rng2 = Sheets(Sheet1).Range("E55:E83")
Set Rng3 = Sheets(Sheet1).Range("K11:K43")

For i = 1 To 3

With Rng & i

.Interior.Color = RGB(255, 255, 0)

End With

Next i

End Sub
 
Upvote 0
I also could Not figure it out. However I would suggest adding another Dim as range see example below:

VBA Code:
Sub test()
Dim Rng1 As Range, Rng2 As Range, Rng3 As Range, AllRng As Range
Dim i As Long
Dim Rng As Variant

Set Rng1 = Sheets("Sheet2").Range("E11:E43")
Set Rng2 = Sheets("Sheet2").Range("E55:E83")
Set Rng3 = Sheets("Sheet2").Range("K11:K43")
Set AllRng = Sheets("Sheet2").Range("E11:E43, E55:E83, K11:K43")
    With AllRng

        .Interior.Color = RGB(255, 255, 0)

    End With

End Sub
 
Upvote 0
VBA Code:
Sub ColorRanges()
'
    Dim Ranges As Range
'
    Set Ranges = Sheets("Sheet2").Range("E11:E43, E55:E83, K11:K43")
'
    With Ranges
        .Interior.Color = RGB(255, 255, 0)
    End With
End Sub
 
Upvote 0
Would something like this work for you ?
VBA Code:
Sub test()

    Dim arrRng(1 To 3) As Range
    Dim sht As Worksheet
    Dim i As Long
    
    Set sht = Worksheets("Sheet1")
    
    With sht
        Set arrRng(1) = .Range("E11:E43")
        Set arrRng(2) = .Range("E55:E83")
        Set arrRng(3) = .Range("K11:K43")
    End With
    
    For i = 1 To UBound(arrRng)
        arrRng(i).Interior.Color = RGB(255, 255, 0)
    Next i

End Sub
 
Upvote 0
Solution
Hi
what about
VBA Code:
Sub test()
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Set rng1 = Range("E11:E43")
    Set rng2 = Range("E55:E83")
    Set rng3 = Range("K11:K43")
    a = Array(rng1, rng2, rng3)
    For i = 1 To 3
        a(i - 1).Select
    Next i
End Sub
 
Upvote 0
thanks all, went with the option suggested by Alex only for the reason that it doesn't require joining the ranges first
 
Upvote 0

Forum statistics

Threads
1,215,514
Messages
6,125,271
Members
449,219
Latest member
daynle

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