Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Need help with dynamic array


I thought I had this down, but I must be forgetting something because
the following code does not work.

I have a range of values in a worksheet which I am trying to read into
an array. My loop is as follows:

Dim myarray() as Double

Range("A1").Select 'Note: assume data is in A1:A500

For i = 1 to 500
number_i = ActiveCell.Value
ReDim Preserve myarray(1 to i)
myarray(i) = number_i
ActiveCell.Offset(i,0).Select
Next

However, this results in a). myarray not expanding at all (dont' know
if values are overwritten or what) and b). The loop appears to execute
in increments which are the length of each re-sized length of myarray
at each iteration instead of the regular steps of '1'.

Does anyone know what I am doing wrong here?

Thanks in advance for any kind help.


--
Peter Bernadyne
------------------------------------------------------------------------
Peter Bernadyne's Profile: http://www.excelforum.com/member.php...fo&userid=7017
View this thread: http://www.excelforum.com/showthread...hreadid=521226

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Need help with dynamic array

Dim myarray() as Double

iLastRow = Cells(Rows.Count,"A").End(xlUp).Row

Redim myarray(1 to iLastRow)

For i = 1 To iLastrow
myarray(i) = Cells(i,"A").Value
Next


--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"Peter Bernadyne"
<Peter.Bernadyne.24h1tm_1142024102.6251@excelfor um-nospam.com wrote in
message news:Peter.Bernadyne.24h1tm_1142024102.6251@excelf orum-nospam.com...

I thought I had this down, but I must be forgetting something because
the following code does not work.

I have a range of values in a worksheet which I am trying to read into
an array. My loop is as follows:

Dim myarray() as Double

Range("A1").Select 'Note: assume data is in A1:A500

For i = 1 to 500
number_i = ActiveCell.Value
ReDim Preserve myarray(1 to i)
myarray(i) = number_i
ActiveCell.Offset(i,0).Select
Next

However, this results in a). myarray not expanding at all (dont' know
if values are overwritten or what) and b). The loop appears to execute
in increments which are the length of each re-sized length of myarray
at each iteration instead of the regular steps of '1'.

Does anyone know what I am doing wrong here?

Thanks in advance for any kind help.


--
Peter Bernadyne
------------------------------------------------------------------------
Peter Bernadyne's Profile:

http://www.excelforum.com/member.php...fo&userid=7017
View this thread: http://www.excelforum.com/showthread...hreadid=521226



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Need help with dynamic array


I see. Thanks very much.

That count code I've used countless times, actually, since I believe
you first showed it to me (3 yrs ago)...I am indebted to you for that,
indeed.

Just out of curiosity, how/when would I resize an array dynamically
using preserve? Would anybody be able to show me how to do this in a
loop?

Thanks again.


--
Peter Bernadyne
------------------------------------------------------------------------
Peter Bernadyne's Profile: http://www.excelforum.com/member.php...fo&userid=7017
View this thread: http://www.excelforum.com/showthread...hreadid=521226

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Need help with dynamic array

It's quite straight-forward Peter.

Dim myarray()

iLastRow = Cells(Rows.Count,"A").End(xlUp).Row

Redim myarray(1 to 1)
'this simply sets the array up

For i = 1 To iLastrow
Redim Preserve myarray(1 To i)
'the array is preserved to the current index, preserving the data
myarray(i) = Cells(i,"A").Value
Next


--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"Peter Bernadyne"
<Peter.Bernadyne.24h3fy_1142026203.2698@excelfor um-nospam.com wrote in
message news:Peter.Bernadyne.24h3fy_1142026203.2698@excelf orum-nospam.com...

I see. Thanks very much.

That count code I've used countless times, actually, since I believe
you first showed it to me (3 yrs ago)...I am indebted to you for that,
indeed.

Just out of curiosity, how/when would I resize an array dynamically
using preserve? Would anybody be able to show me how to do this in a
loop?

Thanks again.


--
Peter Bernadyne
------------------------------------------------------------------------
Peter Bernadyne's Profile:

http://www.excelforum.com/member.php...fo&userid=7017
View this thread: http://www.excelforum.com/showthread...hreadid=521226



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Need help with dynamic array


Thanks very much for that, Bob.

Don't know why my version doesn't work, but I appreciate your
enlightenment of the issue.

Best Regards,

-Pete


--
Peter Bernadyne
------------------------------------------------------------------------
Peter Bernadyne's Profile: http://www.excelforum.com/member.php...fo&userid=7017
View this thread: http://www.excelforum.com/showthread...hreadid=521226

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
Dynamic 2-Dim Array kAVAL Excel Programming 1 July 1st 04 01:07 AM
Dynamic Array Michael168[_82_] Excel Programming 1 June 2nd 04 07:26 AM
Dynamic 2D Array ExcelMonkey[_52_] Excel Programming 4 January 31st 04 09:32 PM
Dynamic Array dan Excel Programming 4 January 27th 04 04:16 PM
see if dynamic array used RobcPettit Excel Programming 2 January 17th 04 12:44 AM


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