Using VBA i make to:
- Set two ranges
- Create an array of all ranges names
- Loop the array in order to apply formatting to each range in the array
BUT on line .Interior.Color = vbRed
i receive the below error:
Run-time error '424': Object required
Code
Sub test()
Dim rng1 As Range, rng2 As Range
Dim strRanges As Variant
Dim i As Long
Set rng1 = Sheet1.Range("A1:D1")
Set rng2 = Sheet2.Range("C5:H5")
strRanges = Split("rng1,rng2", ",")
For i = LBound(strRanges) To UBound(strRanges)
With strRanges(i)
.Interior.Color = vbRed
End With
Next i
End Sub
i have already use:
With Range(strRanges(i))
instead of:
With strRanges(i)
without any luck!
Any help will appreciate.
strRanges = Split("rng1,rng2", ",")instrRanges = Split(rng1.Address & "," & rng2.Address, ",")and useWith Range(strRanges(i)). You cannot build an object range from a string (directly)...strRanges = Split(rng1.Address(External:=True) & "," & rng2.Address(External:=True), ",")line instead. Otherwise, you will paint ranges on active sheet only.if i = 0 then ... With Sheet1.Range(strRanges(i)) ... Else ... Sheet2.Range(strRanges(i))... But your solution is obviously more elegant.Array(rng1, rng2)- add the range objects to the array.