#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Arrays

Hi,
I am trying to produce a sixteen element array from cells in a 4 by 4
section eg (A1:D4) of a larger 16 by 16 grid eg (A1:P16)
If I write

Dim rng1 As Range, rng2 As Range, rng3 As Range
Dim xxx As Variant, yyy As Variant, zzz As Variant

With Worksheets("Sheet1")
Set rng1 = .Range("A1:P1")
Set rng2 = .Range("A1:A16")
Set rng3 = .Range("A1:D1, A2:D2, A3:D3, A4:D4")
End With
xxx = rng1.Value
yyy = rng2.Value
zzz = rng3.Value

then both xxx and yyy produce 16 element arrays. However, zzz produces
only a 4 element array ie the first row.
I can confirm that rng3 describes 16 cells by adding rng3.Select to the
code. The correct area is highlighted.
What else do I have to add to the code?
Thanks in advance,
Ken Rock
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 789
Default Arrays

Hi
Could you not use
Set rng3 = .Range("A1:D4")?

If you want something more flexible you could try
Set rng3 = .Range("A1").Resize(4, 4)

or even
Set rng3 = rng1.Cells(1,1).Resize(4,4)

I don't know why your code doesn't work. Possibly the .Value only picks
up the first area of the range - are the values in xxx those of A1 to
D1?

regards
Paul

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Arrays

Ranges and arrays are not the same thing, and don't behave identically. What
you see is the way that it works, it only picks up contiguous ranges. If you
want non-contiguous ranges in the array, you need to loop and get them all.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Ken Rock" wrote in message
...
Hi,
I am trying to produce a sixteen element array from cells in a 4 by 4
section eg (A1:D4) of a larger 16 by 16 grid eg (A1:P16)
If I write

Dim rng1 As Range, rng2 As Range, rng3 As Range
Dim xxx As Variant, yyy As Variant, zzz As Variant

With Worksheets("Sheet1")
Set rng1 = .Range("A1:P1")
Set rng2 = .Range("A1:A16")
Set rng3 = .Range("A1:D1, A2:D2, A3:D3, A4:D4")
End With
xxx = rng1.Value
yyy = rng2.Value
zzz = rng3.Value

then both xxx and yyy produce 16 element arrays. However, zzz produces
only a 4 element array ie the first row.
I can confirm that rng3 describes 16 cells by adding rng3.Select to the
code. The correct area is highlighted.
What else do I have to add to the code?
Thanks in advance,
Ken Rock



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Arrays

Ken Rock wrote:
Hi,
I am trying to produce a sixteen element array from cells in a 4 by 4
section eg (A1:D4) of a larger 16 by 16 grid eg (A1:P16)
If I write

Dim rng1 As Range, rng2 As Range, rng3 As Range
Dim xxx As Variant, yyy As Variant, zzz As Variant

With Worksheets("Sheet1")
Set rng1 = .Range("A1:P1")
Set rng2 = .Range("A1:A16")
Set rng3 = .Range("A1:D1, A2:D2, A3:D3, A4:D4")
End With
xxx = rng1.Value
yyy = rng2.Value
zzz = rng3.Value

then both xxx and yyy produce 16 element arrays. However, zzz produces
only a 4 element array ie the first row.
I can confirm that rng3 describes 16 cells by adding rng3.Select to the
code. The correct area is highlighted.
What else do I have to add to the code?
Thanks in advance,
Ken Rock

Hi Gents,
Many thanks for your prompt responses to my query.
Paul,
Yes (A1:D4) does work and I shall use it. I don't know why I used the
four groups instead of (A1:D4) but it probably was during my vertical
learning curve on VBA arrays. I must have read an article somewhere on
loading data from a range into the array and used it as a starting point.
Bob,
I still haven't got my head around the properties of the Variant. I
thought an array had to have brackets to define it but if I use, say,
the expression zzz in the Add Watch facility, I see an array. Perhaps
there exists some easily digestible information somewhere on the web
about it.
Again, many thanks for your collective efforts.

Ken Rock
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
Avg Arrays PAL Excel Worksheet Functions 2 February 13th 09 06:02 PM
Arrays Abdul Shakeel Excel Worksheet Functions 1 December 12th 08 10:24 AM
Regarding Arrays Raj Excel Discussion (Misc queries) 5 December 10th 08 03:51 PM
Regarding the Arrays Raj Excel Discussion (Misc queries) 1 December 10th 08 02:26 PM
arrays in arrays jacob Excel Programming 4 August 29th 04 08:40 PM


All times are GMT +1. The time now is 05:35 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"