Home |
Search |
Today's Posts |
#12
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I also recently did some experimenting with [For Each c in MyRange ... Next c
] which process all cells in all areas in the range, but processes each area in turn, so you get all rows in the first area before moving on to the next area, which disturbs the sequence the OP wanted. Yes, this was my first approach when I tried to quickly write some code to help the OP get closer to the desired result. Your code is similar to what I finally came up with and so is why I didn't post any code. What makes it work row by row across all the areas is the use of the Union() function. Alternate approach: Since the location[s] of data is hard coded in the OP's example, I assumed this was the result of how the data was pulled from its source. In this case, the column positions would be fixed as to the field data to be collected. That precludes the possibility of storing the field locations (ie: Col positions) in an array and stepping through each row of data pulling cell refs from the array. This allows getting the data using MyRange.Cells(Row#, ColPosition) as in the following example. <Aircode Dim i As Integer, j As Integer Dim rngSource As Range, rng As Range Dim vCols As Variant Set rngSource = Range("B1:E10") vCols = Array(1, 2, 4) For Each rng In rngSource.Rows For j = LBound(vCols) To UBound(vCols) Debug.Print rngSource.Cells(rng.Row, vCols(j)).Address _ & vbTab & "Value" Next j Next rng Set rngSource = Nothing </Aircode Note that this approach requires less code. -It doesn't require use of the Areas property of the range object nor use of the Union() function to set a ref to the range object, AND requires one less loop. This was where I was originally heading after realizing the Areas approach also required use of the Union() function. I just didn't see any point in posting that since your solution got the job done nicely 'as is'. I guess the OP now has a choice of which approach to go with. With this alternate approach, both the data area (a contiguous range) and column positions (select using Ctrl key if non-contiguous) could also be individually selected by users on the fly, making this approach very flexible and user friendly. Prompt for range area: rngSource = Selection.Address (OR use Application.InputBox() and specify the range data type) Prompt for column positions: Redim the columns array: ReDim vCols(Selection.Columns.Count) Put each column position into the array using a For Each loop like: i = 0 'initialize the counter For Each rng In Selection.Columns vCols(i) = rng.Column: i = i + 1 Next rng regards, -- Garry Free usenet access at http://www.eternal-september.org ClassicVB Users Regroup! comp.lang.basic.visual.misc |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Non-contiguous cells | Excel Discussion (Misc queries) | |||
Copy and Paste LAST ROW of data: non-contiguous Row, contiguous Column | Excel Programming | |||
Looping over non-contiguous column selection | Excel Programming | |||
counting cells that are 0 in a range of non-contiguous cells | Excel Worksheet Functions | |||
How do I count non-contiguous cells? | Excel Worksheet Functions |