Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Named Range to Array Oddity

I've just used a named range to fill an array and have found a
peculiarity that I'm not sure about. The named range was not
contiguous and when assigning it to the array the array only appears to
have used the second section of the range. Is this what normally
happens and is there a work around?

If I gave each part of the original range a separate name could I then
read them easily in to the same array? How?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Named Range to Array Oddity

Hi
could you provide the exact name definition and your assignment code.

Just as a guess: You have to use the areas collection to loop through all
areas of your range

"bloycee" wrote:

I've just used a named range to fill an array and have found a
peculiarity that I'm not sure about. The named range was not
contiguous and when assigning it to the array the array only appears to
have used the second section of the range. Is this what normally
happens and is there a work around?

If I gave each part of the original range a separate name could I then
read them easily in to the same array? How?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Named Range to Array Oddity

I created the named range through the worksheet rather than VB so have
something like "MyNamedRange" is =Sheet1!$H$2:$K$13,Sheet1!$A$2:$D$21
from the Insert, Name, Define menu controls.

The assignment I tried was simply:

Dim MyArray as Variant

MyArray=Range("MyNamedRange").Value

This returned the data from cells H2 to K13 but not that in A2 to D21



Frank Kabel wrote:
Hi
could you provide the exact name definition and your assignment code.

Just as a guess: You have to use the areas collection to loop through

all
areas of your range

"bloycee" wrote:

I've just used a named range to fill an array and have found a
peculiarity that I'm not sure about. The named range was not
contiguous and when assigning it to the array the array only

appears to
have used the second section of the range. Is this what normally
happens and is there a work around?

If I gave each part of the original range a separate name could I

then
read them easily in to the same array? How?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Named Range to Array Oddity

You can't assign a discontiguous range to an array directly

(using a command like varr = Range("MyArray").Value)

You generally can't append values to an array in bulk (as above).

you can loop through all the cells in the range and add them one at a time
to the array.

--
Regards,
Tom Ogilvy


"bloycee" wrote in message
oups.com...
I've just used a named range to fill an array and have found a
peculiarity that I'm not sure about. The named range was not
contiguous and when assigning it to the array the array only appears to
have used the second section of the range. Is this what normally
happens and is there a work around?

If I gave each part of the original range a separate name could I then
read them easily in to the same array? How?



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Named Range to Array Oddity

Hi
then see Tom's reply

"bloycee" wrote:

I created the named range through the worksheet rather than VB so have
something like "MyNamedRange" is =Sheet1!$H$2:$K$13,Sheet1!$A$2:$D$21
from the Insert, Name, Define menu controls.

The assignment I tried was simply:

Dim MyArray as Variant

MyArray=Range("MyNamedRange").Value

This returned the data from cells H2 to K13 but not that in A2 to D21



Frank Kabel wrote:
Hi
could you provide the exact name definition and your assignment code.

Just as a guess: You have to use the areas collection to loop through

all
areas of your range

"bloycee" wrote:

I've just used a named range to fill an array and have found a
peculiarity that I'm not sure about. The named range was not
contiguous and when assigning it to the array the array only

appears to
have used the second section of the range. Is this what normally
happens and is there a work around?

If I gave each part of the original range a separate name could I

then
read them easily in to the same array? How?






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Named Range to Array Oddity

This seemed to work for me in very limited testing

Sub test()
Dim v, i as Long, aCnt as long

'ActiveWorkbook.Names.Add Name:="myname", _
'RefersTo:=Range("Sheet1!$H$2:$K$13,Sheet1!$A$2:$D $21")
'
'For Each cell In Range("myname")
'i = i + 1
'cell.Value = i
'Next

With Range("myname")
aCnt = .Areas.Count
ReDim v(1 To aCnt) As Variant
For i = 1 To aCnt
v(i) = .Areas(i).Value
Next
End With

'test it
Dim rw As Long, cl As Long
For i = 1 To UBound(v)
Debug.Print "Area " & i
For rw = 1 To UBound(v(i), 1)
For cl = 1 To UBound(v(i), 2)
Debug.Print v(i)(rw, cl)
Next
Next
Next

End Sub

Regards,
Peter


"bloycee" wrote in message
oups.com...
I created the named range through the worksheet rather than VB so have
something like "MyNamedRange" is =Sheet1!$H$2:$K$13,Sheet1!$A$2:$D$21
from the Insert, Name, Define menu controls.

The assignment I tried was simply:

Dim MyArray as Variant

MyArray=Range("MyNamedRange").Value

This returned the data from cells H2 to K13 but not that in A2 to D21



Frank Kabel wrote:
Hi
could you provide the exact name definition and your assignment code.

Just as a guess: You have to use the areas collection to loop through

all
areas of your range

"bloycee" wrote:

I've just used a named range to fill an array and have found a
peculiarity that I'm not sure about. The named range was not
contiguous and when assigning it to the array the array only

appears to
have used the second section of the range. Is this what normally
happens and is there a work around?

If I gave each part of the original range a separate name could I

then
read them easily in to the same array? How?





  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Named Range to Array Oddity

Thanks all,

I'll give Peter's idea a go.

Cheers.

Reply
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
Using Named Range within an Array Formula Ivor Davies Excel Discussion (Misc queries) 2 August 25th 09 11:15 AM
Array as a "named range" - formula ok in cells, but error as "named range" tskogstrom Excel Discussion (Misc queries) 11 December 28th 06 04:44 PM
Named range into an array Frigster Excel Worksheet Functions 2 September 6th 06 07:08 PM
Array to named range conversion... i-Zapp Excel Discussion (Misc queries) 4 October 25th 05 09:09 PM
Defined named range to array MattShoreson[_2_] Excel Programming 1 December 4th 03 10:06 AM


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

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

About Us

"It's about Microsoft Excel"