Iterating over a Union range

gravanoc

Active Member
Joined
Oct 20, 2015
Messages
351
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I have an issue iterating over a couple non-contiguous union ranges.
The reportRng is also non-contiguous, and could be specified as a union of B4:J4 & M4:N4. The problem is that trendRng.Cells(2, 1) will reference A3 instead of E7. The same happens with reportRng.
I think using Areas might help address the problem, but I'm not sure how.

VBA Code:
    Set reportRng = individualLO.ListRows(1).Range.SpecialCells(xlCellTypeConstants)
    
    For i = 0 To x - 1
        
        With trendShts(i)
            Set trendRng = Union(.Range("A1:A2"), .Range("E7"), .Range("E17:E22"), .Range("E33:E34"))
        End With

        For j = 0 To trendRng.Areas.count - 1
            reportRng.Cells(i + 1, j + 1).Value = trendRng.Cells(j + 1, 1).Value
        Next j

        individualLO.ListRows.Add
        
    Next i
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
This doesn't sound like a great data layout. Given that both ranges are discontiguous and they aren't even the same shape, I'd suggest you populate an array with the data from trendRng using a counter variable and a For Each cell in trendRng.Cells loop. Then do the same process in reverse with the output range using another For Each loop to iterate the cells and a counter to determine which array value to use.
 
Upvote 0
Solution
This doesn't sound like a great data layout. Given that both ranges are discontiguous and they aren't even the same shape, I'd suggest you populate an array with the data from trendRng using a counter variable and a For Each cell in trendRng.Cells loop. Then do the same process in reverse with the output range using another For Each loop to iterate the cells and a counter to determine which array value to use.
Thanks, I will give it a try and let you know.
 
Upvote 0

Forum statistics

Threads
1,224,531
Messages
6,179,379
Members
452,907
Latest member
Roland Deschain

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