View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default reading an arbitrary selection into a 1D array

Another way might be

Dim myArray() as Variant
Dim rng as Range
Dim i as long
set rng = Selection
Redim myArray(1 to rng.Count)
i = 0
for each cell in rng
i = i + 1
myArray(i) = cell.Value
Next

so as a test I filled the sheet so each cell displayed its address, selected
some cells at random, then ran this:

Sub Tester1()
Dim myArray() As Variant
Dim rng As Range
Dim i As Long
Set rng = Selection
Debug.Print rng.Address
ReDim myArray(1 To rng.Count)
i = 0
For Each cell In rng
i = i + 1
myArray(i) = cell.Value
Debug.Print i, myArray(i)
Next


End Sub


Which produced:
$A$1,$D$8:$E$9,$C$3,$E$1,$C$13:$D$14
1 A1
2 D8
3 E8
4 D9
5 E9
6 C3
7 E1
8 C13
9 D13
10 C14
11 D14

--
Regards,
Tom Ogilvy


"Jamie Martin" wrote in message
...
Hi,

I want to let my user select any combination of cells, and then read the
values from them into a one-dimensional array. How can I do this? Some

parts
of the selection may be two-dimensional, while other parts may be
one-dimensional. I also need to cycle through each seperate selected area.

Thanks for any help,

Jamie