Ah ... so we're both learning! It's great when the ng's work like this
<smile>
After reading your reply I did some more looking. The collection doesn't
care what you put into it ... you can put the range into the collection
if you want to: [ coll.Add r ] would add the range itself as the
collection item. Likewise, [ coll.Add arr ] adds the array as the item.
The problem seems to be that [ arr = r ] doesn't pick up the succeding
regions of a Union range.
set r = union(range("a1:a7"),range("c1:c7"))
For Each c In r
coll.Add c.Value, CStr(c.Value)
Next c
where c is a variant will add all the cell values of a union range to
the collection; but it appears to add them in area order - so when you
walk through the collection you will get all column a, then column c;
whereas [ set r = range("a1:c7") ] I believe would give you the three
columns in row 1, then row 2, etc.
I did some digging, and learned how to discover the upper bound of the
second (nth) array dimension ... the code below illustrates what I
found:
Sub Builder(r As Range)
Dim arr
arr = r
On Error Resume Next
For i = 1 To UBound(arr)
j = UBound(arr, 2)
if j = Empty then ' no multi-dimensions
j = 1
End If
For j = 1 To j
coll.Add arr(i, j), CStr(arr(i, j))
Next j
Next i
End Sub
Another possibility is to use the range directly and not even bother
with the variant array:
For i = 1 To r.Rows.Count
For j = 1 To r.Columns.Count
'coll.Add r.Cells(i, j).Value, CStr(r.Cells(i, j).Value)
coll.Add r.Cells(i, j).Value
Next j
Next i
The line I commented out causes an error if there is a duplicate value
in any cell, causing that cell to not be added to the collection.
Because of the [ On Error Resume Next ] any duplicate values are skipped
without warning.
This method should process union ranges as you expect.
So ... there are many options here.
Good luck!
Glad I was able to help.
--
Clif
I have looked at this some more using your suggestions of the locals
windows and I think I have figured out part of the problem. The unions
method does not seem to be compatible with the collections method. If
I make a union of the two discontinuous ranges the "array" that is
created is only one element per item (it only contains the data from
the first part of the joined range). But if I expand the range by
using something like set r = range(r,r.offset(,2)) - which would
include the column between the 2 desired ranges - then I end up with 3
elements per item with the arr variable.
I also do see data being placed in the collection using you simplified
example code - I think you need to make your selection as a1:a7 in
order for it populate something into the collection.
<
[email protected]> wrote in message
Thanks for the suggestions but I have resigned myself to concatenating
the 2 range values, loading this into the collection, and then
breaking then apart again at the other end and placing them in the
corresponding cells.
Using typename for arr just returned "variant" which didn't seem to
really help much.
** I found some time to try running your code ... and I'm puzzled:
When
I ran your code as modified (below), nothing was added to the
collection????
I created a new workbook and populated A1:C7 with sample data then
selected A1.
I opened the VBE and pasted this code with a breakpoint set at <bp>,
and
ran with F5. Opening the locals window and single-stepping from the
breakpoint confirmed that the union created a range with two cells;
but
the arr = r resulted in a simple variant with the contents of A1; and
nothing was added to the collection. So: I don't seem to be able to
see
what you are seeing.
Sub main_merge()
Set coll = New Collection
Dim r As Range
Dim nme As Name
Dim rngname As String
'Set coll = Nothing
'For Each c In Range("Category").Cells
'Set coll = New Collection
'rngname = c.Value
'rngvalue = c.Value + "_"
'MsgBox rngvalue
'For Each nme In ActiveWorkbook.Names
'If InStr(1, nme.Name, rngvalue) Then
' Set r = Range(nme.Name)
' Set r = Union(r, Range(nme.Name).Offset(0, 3))
<bp> Set r = Selection
Set r = Union(r, r.Offset(0, 2))
Call Builder(r)
'End If
'Next nme
'Call Displayer(r, rngname)
''Set coll = Nothing
'Next c
End Sub
Sub Builder(r As Range)
Dim arr
arr = r
On Error Resume Next
For i = 1 To UBound(arr)
coll.Add arr(i, 1), CStr(arr(i, 1))
Next i
End Sub
(clare reads his mail with moe, nomail feeds the bit bucket
--
Clif McIrvin
(clare reads his mail with moe, nomail feeds the bit bucket
- Hide quoted text -
- Show quoted text -