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

I have an array, which starts out like this:

PWL = Array("Data1", "Data2", "Data3")


Now I would like to add a value to this array?
As if it had started out like this:

PWL = Array("Data1", "Data2", "Data3", "Data4")


How do I add the "Data4" to the original array???


CE
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Adding to Array

Dim PWL As Variant
PWL = Array("Data1", "Data2", "Data3")
ReDim Preserve PWL(0 To 3)
PWL(3) = "Data4"
--
Jim Cone
Portland, Oregon USA
http://www.mediafire.com/PrimitiveSoftware
(free and commercial excel programs)



"Charlotte E."
wrote in message
...
I have an array, which starts out like this:

PWL = Array("Data1", "Data2", "Data3")


Now I would like to add a value to this array?
As if it had started out like this:

PWL = Array("Data1", "Data2", "Data3", "Data4")


How do I add the "Data4" to the original array???


CE



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default Adding to Array

"Jim Cone" wrote:
"Charlotte E."
I have an array, which starts out like this:
PWL = Array("Data1", "Data2", "Data3")
Now I would like to add a value to this array?
As if it had started out like this:
PWL = Array("Data1", "Data2", "Data3", "Data4")


Dim PWL As Variant
PWL = Array("Data1", "Data2", "Data3")
ReDim Preserve PWL(0 To 3)
PWL(3) = "Data4"


But if Option Base 1 were set, we would need ReDim Preserve PWL(1 to 4).

More generally:

ReDim Preserve PWL(LBound(PWL) to UBound(PWL)+1)

PS: If this is in a loop, I don't know how inefficient it is to increase an
array by just one. I usually increase by larger amounts, e.g.
2*UBound(PWL), and maintain variables, e.g. nPWL and nPWLmax, to track the
"active region" of the array. If/when it comes time to copy the array to a
worksheet, I would use Range("a1").Resize(,nPWL) = PWL.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,549
Default Adding to Array


Good points.
I tend to limit the depth of a newsgroup response to the corresponding effort/detail put into the
question.
Jim Cone


"joeu2004"
wrote in message
...
"Jim Cone" wrote:
"Charlotte E."
I have an array, which starts out like this:
PWL = Array("Data1", "Data2", "Data3")
Now I would like to add a value to this array?
As if it had started out like this:
PWL = Array("Data1", "Data2", "Data3", "Data4")


Dim PWL As Variant
PWL = Array("Data1", "Data2", "Data3")
ReDim Preserve PWL(0 To 3)
PWL(3) = "Data4"


But if Option Base 1 were set, we would need ReDim Preserve PWL(1 to 4).

More generally:

ReDim Preserve PWL(LBound(PWL) to UBound(PWL)+1)

PS: If this is in a loop, I don't know how inefficient it is to increase an array by just one. I
usually increase by larger amounts, e.g. 2*UBound(PWL), and maintain variables, e.g. nPWL and
nPWLmax, to track the "active region" of the array. If/when it comes time to copy the array to a
worksheet, I would use Range("a1").Resize(,nPWL) = PWL.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 538
Default Adding to Array

joeu2004 wrote:

"Jim Cone" wrote:
"Charlotte E."
I have an array, which starts out like this:
PWL = Array("Data1", "Data2", "Data3")
Now I would like to add a value to this array?
As if it had started out like this:
PWL = Array("Data1", "Data2", "Data3", "Data4")


Dim PWL As Variant
PWL = Array("Data1", "Data2", "Data3")
ReDim Preserve PWL(0 To 3)
PWL(3) = "Data4"


But if Option Base 1 were set, we would need ReDim Preserve PWL(1 to 4).

More generally:

ReDim Preserve PWL(LBound(PWL) to UBound(PWL)+1)


No need to specify the lower bound in this case:
ReDim Preserve PWL(UBound(PWL)+1)

--
Error -53. We'd tell you what that means, but we don't know either.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 160
Default Adding to Array

And that concludes my lecture on arrays for today - thanks guys :-)

CE


Den 23.10.2011 17:37, Charlotte E. skrev:
I have an array, which starts out like this:

PWL = Array("Data1", "Data2", "Data3")


Now I would like to add a value to this array?
As if it had started out like this:

PWL = Array("Data1", "Data2", "Data3", "Data4")


How do I add the "Data4" to the original array???


CE

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
Checking array to see if value exists before adding to array Wullie Excel Programming 4 June 19th 09 09:00 AM
Adding rows in the array diamond Excel Programming 0 April 15th 09 02:39 PM
Adding values to 2-dim array Tod Excel Programming 4 November 28th 04 08:13 AM
Adding to an array Otto Moehrbach[_6_] Excel Programming 5 October 24th 04 07:50 PM
Adding an Array James Stephens[_3_] Excel Programming 2 January 14th 04 12:51 PM


All times are GMT +1. The time now is 05:15 PM.

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"