Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Arrays in macros

Within a sub procedure two other sub procedures are called consecutively.
The first sub procedure feeds values into an array.
I need this array with these values to then be used in the second sub
procedure.

As soon as the first sub procedure is complete the array becomes empty and
passes an empty array to the second sub procedure.
Thus givign me zero for all my calculations in the second array.

Please assist.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Arrays in macros

Either declare the array variable in the module declaration, that is before
any macros, or pass it as a parameter to the second macro.

Air-coded

Sub Macro1()
Dim myArray

myArray = Array(1,2,3)
Macro2 myArray

End Sub

Sub Macro2(ary As Variant)

msgbox ary(1)

End Sub

--
HTH

Bob Phillips

"Bradley" wrote in message
...
Within a sub procedure two other sub procedures are called consecutively.
The first sub procedure feeds values into an array.
I need this array with these values to then be used in the second sub
procedure.

As soon as the first sub procedure is complete the array becomes empty and
passes an empty array to the second sub procedure.
Thus givign me zero for all my calculations in the second array.

Please assist.



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

The arrays need to be public and static. Just DIM them outside the subs, not
inside the subs and the values will "live" from sub call to sub call

Have a good day
--
Gary''s Student


"Bradley" wrote:

Within a sub procedure two other sub procedures are called consecutively.
The first sub procedure feeds values into an array.
I need this array with these values to then be used in the second sub
procedure.

As soon as the first sub procedure is complete the array becomes empty and
passes an empty array to the second sub procedure.
Thus givign me zero for all my calculations in the second array.

Please assist.

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

Gary,

They only need to be public if used across modules. If used in separate
macros in the same module, private is fine (preferable?).

--
HTH

Bob Phillips

"Gary''s Student" wrote in message
...
The arrays need to be public and static. Just DIM them outside the subs,

not
inside the subs and the values will "live" from sub call to sub call

Have a good day
--
Gary''s Student


"Bradley" wrote:

Within a sub procedure two other sub procedures are called

consecutively.
The first sub procedure feeds values into an array.
I need this array with these values to then be used in the second sub
procedure.

As soon as the first sub procedure is complete the array becomes empty

and
passes an empty array to the second sub procedure.
Thus givign me zero for all my calculations in the second array.

Please assist.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Arrays in macros

Unfortunately the arrays vary any size for different times the subs are run.
So I have them as PUBLIC open array initially and then REDIM them for the
correct size just before populating them in the first sub.
I have tried to make this sub a function instead to pass the array with its
"new" info back to the initial sub and then pass this to the second sub where
the next series of calculations take place.
Unfortunately, it still reverts back to an empty array as it returns to the
initial sub before even being passed to the second sub.

Is there a way of stating the array in the sub name so that it passes the
array back with the new data?

"Gary''s Student" wrote:

The arrays need to be public and static. Just DIM them outside the subs, not
inside the subs and the values will "live" from sub call to sub call

Have a good day
--
Gary''s Student


"Bradley" wrote:

Within a sub procedure two other sub procedures are called consecutively.
The first sub procedure feeds values into an array.
I need this array with these values to then be used in the second sub
procedure.

As soon as the first sub procedure is complete the array becomes empty and
passes an empty array to the second sub procedure.
Thus givign me zero for all my calculations in the second array.

Please assist.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Arrays in macros

I did try and declare the array as a public variable prior to any subs but
this requires them to have either a fixed size or to be totally variable, ie
myArray(5) or myArray().
So I set them initially as follows:
PUBLIC myArray() as integer
and then in the main sub redefined them as follows:
REDIM myArray(Var_Size) as integer

This array then populates fine in the first sub, within the main sub, but as
soon as the first sub is completed and you return to the main sub the array
empties.
Which means an empty array is carried into the second sub, within the main
sub.

Any other suggestions on how to pass the populated array back to the main sub?

Thanks for the initial suggestion.

"Bob Phillips" wrote:

Either declare the array variable in the module declaration, that is before
any macros, or pass it as a parameter to the second macro.

Air-coded

Sub Macro1()
Dim myArray

myArray = Array(1,2,3)
Macro2 myArray

End Sub

Sub Macro2(ary As Variant)

msgbox ary(1)

End Sub

--
HTH

Bob Phillips

"Bradley" wrote in message
...
Within a sub procedure two other sub procedures are called consecutively.
The first sub procedure feeds values into an array.
I need this array with these values to then be used in the second sub
procedure.

As soon as the first sub procedure is complete the array becomes empty and
passes an empty array to the second sub procedure.
Thus givign me zero for all my calculations in the second array.

Please assist.




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Arrays in macros

I think that solves my problem.
The subs I was calling were in seperate modules.
I should just group them in the same module.

Will let you know.
Thanks Bob and Gary

"Bob Phillips" wrote:

Gary,

They only need to be public if used across modules. If used in separate
macros in the same module, private is fine (preferable?).

--
HTH

Bob Phillips

"Gary''s Student" wrote in message
...
The arrays need to be public and static. Just DIM them outside the subs,

not
inside the subs and the values will "live" from sub call to sub call

Have a good day
--
Gary''s Student


"Bradley" wrote:

Within a sub procedure two other sub procedures are called

consecutively.
The first sub procedure feeds values into an array.
I need this array with these values to then be used in the second sub
procedure.

As soon as the first sub procedure is complete the array becomes empty

and
passes an empty array to the second sub procedure.
Thus givign me zero for all my calculations in the second array.

Please assist.




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

Although your problem seems solved now, you might be interested to read
Knowledgebase article ID 843144 for background info


Jan Bart

"Bradley" wrote:

I think that solves my problem.
The subs I was calling were in seperate modules.
I should just group them in the same module.

Will let you know.
Thanks Bob and Gary

"Bob Phillips" wrote:

Gary,

They only need to be public if used across modules. If used in separate
macros in the same module, private is fine (preferable?).

--
HTH

Bob Phillips

"Gary''s Student" wrote in message
...
The arrays need to be public and static. Just DIM them outside the subs,

not
inside the subs and the values will "live" from sub call to sub call

Have a good day
--
Gary''s Student


"Bradley" wrote:

Within a sub procedure two other sub procedures are called

consecutively.
The first sub procedure feeds values into an array.
I need this array with these values to then be used in the second sub
procedure.

As soon as the first sub procedure is complete the array becomes empty

and
passes an empty array to the second sub procedure.
Thus givign me zero for all my calculations in the second array.

Please assist.




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
weird saving of a document with macros resulting with macros being transfered to the copy alfonso gonzales Excel Programming 0 December 12th 04 09:19 PM
convert lotus 123w macros to excel macros rpiescik[_2_] Excel Programming 1 September 19th 04 12:41 PM
Macro Size Limit / open macros with macros? andycharger[_7_] Excel Programming 6 February 13th 04 02:00 PM
Macros not appearing in the Tools Macro Macros list hglamy[_2_] Excel Programming 5 October 24th 03 09:10 AM
Suppress the Disable Macros / Enable Macros Dialog Shoji Karai Excel Programming 5 September 24th 03 03:10 AM


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