LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Looping through Non contiguous cells in a row

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Non-contiguous cells Kokomojo Excel Discussion (Misc queries) 2 November 6th 07 04:09 PM
Copy and Paste LAST ROW of data: non-contiguous Row, contiguous Column Sam via OfficeKB.com Excel Programming 8 November 5th 07 07:18 PM
Looping over non-contiguous column selection Jim Hagan Excel Programming 2 June 14th 07 07:16 PM
counting cells that are 0 in a range of non-contiguous cells Mark Excel Worksheet Functions 9 March 14th 07 02:45 PM
How do I count non-contiguous cells? broush Excel Worksheet Functions 5 May 30th 06 11:16 PM


All times are GMT +1. The time now is 12:59 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"