ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problem collecting Range Values (https://www.excelbanter.com/excel-programming/357834-problem-collecting-range-values.html)

richcoleuk

Problem collecting Range Values
 

I am trying to collect and Range, that is made up of a number o
"sub-ranges" if you like.

I create three ranges the normal way by giving cell references. Thi
works fine and my three ranges hold the values that I need in th
Value2 attribute.

The ranges aren't next to each other and I want to join them into on
range, so I've used the Union() method. This combines the ranges an
if I now use the select method this highlights my range and you can se
that it is the range I'm after.

However the value2 attribute of this combined range, doesn't hold al
the values covered by the range? In fact it only contains values fro
the first range I pass into Union()

Any ideas? Or other solutions to collecting non-contigous ranges

--
richcoleu
-----------------------------------------------------------------------
richcoleuk's Profile: http://www.excelforum.com/member.php...fo&userid=3310
View this thread: http://www.excelforum.com/showthread.php?threadid=52912


Martin

Problem collecting Range Values
 
You're right - the locals window only shows Value2 for the first range - weird!

However, if you use Value you can pick up all the cells in the range, eg:

Dim myRange As Range
Dim myCell As Range
Set myRange = Application.Union(Range("A1:A3"), Range("A5:A6"),
Range("A8:A10"))
For Each myCell In myRange
MsgBox myCell.Value
Next


"richcoleuk" wrote:


I am trying to collect and Range, that is made up of a number of
"sub-ranges" if you like.

I create three ranges the normal way by giving cell references. This
works fine and my three ranges hold the values that I need in the
Value2 attribute.

The ranges aren't next to each other and I want to join them into one
range, so I've used the Union() method. This combines the ranges and
if I now use the select method this highlights my range and you can see
that it is the range I'm after.

However the value2 attribute of this combined range, doesn't hold all
the values covered by the range? In fact it only contains values from
the first range I pass into Union()

Any ideas? Or other solutions to collecting non-contigous ranges?


--
richcoleuk
------------------------------------------------------------------------
richcoleuk's Profile: http://www.excelforum.com/member.php...o&userid=33103
View this thread: http://www.excelforum.com/showthread...hreadid=529123



richcoleuk[_2_]

Problem collecting Range Values
 

Okay thanks for that,

How ever I am using some C# behind excel and I need to be able to pas
in a range that has a value2 attribute that holds all the data from th
multiple ranges.

How can I make sure this 1 range has all the data stored in Value2

--
richcoleu
-----------------------------------------------------------------------
richcoleuk's Profile: http://www.excelforum.com/member.php...fo&userid=3310
View this thread: http://www.excelforum.com/showthread.php?threadid=52912


[email protected]

Problem collecting Range Values
 
Hi
You could output an array of variants.
Each range object has an Areas property to count the disconnected bits
of the range. for your three areas you could try

Function Tester() as Variant
Dim OutputValues() as Variant
AreaCount = Selection.Areas.Count
ReDim OutputValues(1 to AreaCount)
for i = 1 to AreaCount
OutputValues(i) =Selection.Areas(i).Value
next i
Tester = OutputValues
end function

Then OutPutValues(2) would be the array of values in area 2 of the
selection.

regards
Paul


richcoleuk[_3_]

Problem collecting Range Values
 

Okay I think I'm nearly there. The only trouble I have now is that th
C# method I need to use expects a Range[] and i don't think it like
being passed a Variant, in the example you gave Teste

--
richcoleu
-----------------------------------------------------------------------
richcoleuk's Profile: http://www.excelforum.com/member.php...fo&userid=3310
View this thread: http://www.excelforum.com/showthread.php?threadid=52912


richcoleuk[_4_]

Problem collecting Range Values
 

No one have anyideas how i can convert to a Range[]


--
richcoleuk
------------------------------------------------------------------------
richcoleuk's Profile: http://www.excelforum.com/member.php...o&userid=33103
View this thread: http://www.excelforum.com/showthread...hreadid=529123


[email protected]

Problem collecting Range Values
 
Hi
I sent you solution privately, but just for this thread:

An array of Ranges output:

Function Tester2() As Variant
Dim OutputRanges() As Range, i As Integer
AreaCount = Selection.Areas.Count
ReDim OutputRanges(1 To AreaCount)
For i = 1 To AreaCount
Set OutputRanges(i) = Selection.Areas(i)
Next i
Tester2 = OutputRanges
End Function

Public Sub test()
Dim myValues As Variant
For i = 1 To UBound(Tester2)
Set Testarea = Tester2(i)
MsgBox Testarea.Address
Next i
End Sub

A Collection of Ranges output:

Function Tester2() As Collection
Dim OutputRanges As New Collection, i As Integer
AreaCount = Selection.Areas.Count
For i = 1 To AreaCount
OutputRanges.Add Selection.Areas(i), "Area" & i
Next i
Set Tester2 = OutputRanges
End Function

Public Sub test()
Dim Testarea as Range
Dim myValues As Variant
For i = 1 To Tester2.Count
Set Testarea = Tester2("Area" & i)
MsgBox Testarea.Address
Next i
End Sub

regards
Paul



All times are GMT +1. The time now is 11:08 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com