Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel,microsoft.public.excel.programming
external usenet poster
 
Posts: 62
Default A more elegant solution to grab a range?

Hello

I am looking for a more elegant (and shorter) solution to grab the
content of an excel range into an array:

Dim grab
Dim value()
grab = Range("A1:A100").Value2
ReDim value(1 To UBound(grab))
For i = 1 To UBound(grab)
value(i) = grab(i, 1)
Next i

I could just use the array grab but then I have to carry a two
dimensional syntax all the way, although I know that grab only contains
one column. Or is there a simple instruction to "destroy " one of the
dimensions of an array?

Thanks ina advance
Charles

  #2   Report Post  
Posted to microsoft.public.excel,microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default A more elegant solution to grab a range?

Dim myArr As Variant
myArr = Application.Transpose(Range("a1:a100").Value2)

IIRC, this will break in some versions of excel if you have more than 5461
elements.

Charles wrote:

Hello

I am looking for a more elegant (and shorter) solution to grab the
content of an excel range into an array:

Dim grab
Dim value()
grab = Range("A1:A100").Value2
ReDim value(1 To UBound(grab))
For i = 1 To UBound(grab)
value(i) = grab(i, 1)
Next i

I could just use the array grab but then I have to carry a two
dimensional syntax all the way, although I know that grab only contains
one column. Or is there a simple instruction to "destroy " one of the
dimensions of an array?

Thanks ina advance
Charles


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel,microsoft.public.excel.programming
external usenet poster
 
Posts: 62
Default A more elegant solution to grab a range?

I am impressed. Very efficient!

thanks
Charles


Dave Peterson wrote:
Dim myArr As Variant
myArr = Application.Transpose(Range("a1:a100").Value2)

IIRC, this will break in some versions of excel if you have more than 5461
elements.

Charles wrote:

Hello

I am looking for a more elegant (and shorter) solution to grab the
content of an excel range into an array:

Dim grab
Dim value()
grab = Range("A1:A100").Value2
ReDim value(1 To UBound(grab))
For i = 1 To UBound(grab)
value(i) = grab(i, 1)
Next i

I could just use the array grab but then I have to carry a two
dimensional syntax all the way, although I know that grab only contains
one column. Or is there a simple instruction to "destroy " one of the
dimensions of an array?

Thanks ina advance
Charles


--

Dave Peterson


  #4   Report Post  
Posted to microsoft.public.excel,microsoft.public.excel.programming
external usenet poster
 
Posts: 200
Default A more elegant solution to grab a range?

You can also readily "destroy" the second dimension of a 2-D
*horizontal* array. The most commonly referred to way is

myArr = Application.Transpose(Application.Transpose(Range( "a1:z1").Value2))

Another is

myArr = Application.Index(Range("a1:z1").Value2, 1, 0)

Alan Beban

Charles wrote:
I am impressed. Very efficient!

thanks
Charles


Dave Peterson wrote:
Dim myArr As Variant
myArr = Application.Transpose(Range("a1:a100").Value2)

IIRC, this will break in some versions of excel if you have more than 5461
elements.

Charles wrote:
Hello

I am looking for a more elegant (and shorter) solution to grab the
content of an excel range into an array:

Dim grab
Dim value()
grab = Range("A1:A100").Value2
ReDim value(1 To UBound(grab))
For i = 1 To UBound(grab)
value(i) = grab(i, 1)
Next i

I could just use the array grab but then I have to carry a two
dimensional syntax all the way, although I know that grab only contains
one column. Or is there a simple instruction to "destroy " one of the
dimensions of an array?

Thanks ina advance
Charles

--

Dave Peterson


  #5   Report Post  
Posted to microsoft.public.excel,microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default A more elegant solution to grab a range?

Hi

Public Sub Test()
Dim MyArray() As Variant
Dim rngTest As Range
Dim RowNum As Integer
Dim ColNum As Integer


Set rngTest = [TestRange]
RowNum = rngTest.Rows.Count
ColNum = rngTest.Columns.Count
ReDim MyArray(RowNum, ColNum)
MyArray = rngTest


End Sub

TestRange is a dynamic named range, defined in workbook, something like
=OFFSET(Sheet1!$A$1,1,,COUNTA(Sheet1!$A:$A)-1,COUNTA(Sheet1!$1;$1))


Arvi Laanemets



"Charles" wrote in message
oups.com...
Hello

I am looking for a more elegant (and shorter) solution to grab the
content of an excel range into an array:

Dim grab
Dim value()
grab = Range("A1:A100").Value2
ReDim value(1 To UBound(grab))
For i = 1 To UBound(grab)
value(i) = grab(i, 1)
Next i

I could just use the array grab but then I have to carry a two
dimensional syntax all the way, although I know that grab only contains
one column. Or is there a simple instruction to "destroy " one of the
dimensions of an array?

Thanks ina advance
Charles





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
Elegant solution for two comparisons Joe Murphy[_2_] Excel Discussion (Misc queries) 9 January 25th 08 10:42 PM
Need More Elegant Solution - Simple Program [email protected] Excel Programming 4 November 14th 06 06:02 PM
Is there an elegant solution to this table? [email protected] Excel Discussion (Misc queries) 3 July 9th 06 01:31 PM
Working Hours (formula & graph) - any elegant solution? markx Excel Worksheet Functions 1 March 29th 06 02:02 PM
elegant programmed solution to local minima with constraints Bill[_32_] Excel Programming 0 July 28th 05 10:04 PM


All times are GMT +1. The time now is 06:43 PM.

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"