Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default How to keep an Array 'alive'

Hi there,

I have a Sub in a form, which runs when a commandbutton is pressed. In the
Sub an array is dimmed and filled with values.

e.g. Dim NANames(1 To 20, 0 To 2)

I need the values of the Array in another Sub, but the array is only
available in the specific Sub when created. Declaring it Public is not
possible with an array, so I need another way of doing this.

Anyone ?

Thanks !


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default How to keep an Array 'alive'

You could make it a public variable within a Standard Module

--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Fred" wrote in message
...
Hi there,

I have a Sub in a form, which runs when a commandbutton is pressed. In the
Sub an array is dimmed and filled with values.

e.g. Dim NANames(1 To 20, 0 To 2)

I need the values of the Array in another Sub, but the array is only
available in the specific Sub when created. Declaring it Public is not
possible with an array, so I need another way of doing this.

Anyone ?

Thanks !




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default How to keep an Array 'alive'

Rob,

thanks for the quick reaction ! But....
I declared it public in the main module which starts (load/show) the form on
which the array is filled. At the end of code execution of the Sub on the
form the Array is alive and filled with the correct values, but when the Sub
ends (the form is still active) the values disappear...


???

"Rob van Gelder" wrote in message
...
You could make it a public variable within a Standard Module

--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Fred" wrote in message
...
Hi there,

I have a Sub in a form, which runs when a commandbutton is pressed. In

the
Sub an array is dimmed and filled with values.

e.g. Dim NANames(1 To 20, 0 To 2)

I need the values of the Array in another Sub, but the array is only
available in the specific Sub when created. Declaring it Public is not
possible with an array, so I need another way of doing this.

Anyone ?

Thanks !






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default How to keep an Array 'alive'

Here's an example:

Module Code:

Public arr() As String

Sub test()
Dim i As Long
UserForm1.Show '(assuming your userform is named UserForm1)

For i = LBound(arr) To UBound(arr)
Debug.Print arr(i)
Next
End Sub


UserForm1 Code :

Private Sub UserForm_Initialize()
Dim i As Long

ReDim arr(50)

For i = LBound(arr) To UBound(arr)
arr(i) = "string " & i
Next
End Sub


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Fred" wrote in message
...
Rob,

thanks for the quick reaction ! But....
I declared it public in the main module which starts (load/show) the form

on
which the array is filled. At the end of code execution of the Sub on the
form the Array is alive and filled with the correct values, but when the

Sub
ends (the form is still active) the values disappear...


???

"Rob van Gelder" wrote in message
...
You could make it a public variable within a Standard Module

--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Fred" wrote in message
...
Hi there,

I have a Sub in a form, which runs when a commandbutton is pressed. In

the
Sub an array is dimmed and filled with values.

e.g. Dim NANames(1 To 20, 0 To 2)

I need the values of the Array in another Sub, but the array is only
available in the specific Sub when created. Declaring it Public is not
possible with an array, so I need another way of doing this.

Anyone ?

Thanks !








  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default How to keep an Array 'alive'

Rob,

it works ! Thanks.
I had forgotten to take the Dim statement out of the Sub on the form...


"Rob van Gelder" wrote in message
...
Here's an example:

Module Code:

Public arr() As String

Sub test()
Dim i As Long
UserForm1.Show '(assuming your userform is named UserForm1)

For i = LBound(arr) To UBound(arr)
Debug.Print arr(i)
Next
End Sub


UserForm1 Code :

Private Sub UserForm_Initialize()
Dim i As Long

ReDim arr(50)

For i = LBound(arr) To UBound(arr)
arr(i) = "string " & i
Next
End Sub


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Fred" wrote in message
...
Rob,

thanks for the quick reaction ! But....
I declared it public in the main module which starts (load/show) the

form
on
which the array is filled. At the end of code execution of the Sub on

the
form the Array is alive and filled with the correct values, but when the

Sub
ends (the form is still active) the values disappear...


???

"Rob van Gelder" wrote in

message
...
You could make it a public variable within a Standard Module

--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Fred" wrote in message
...
Hi there,

I have a Sub in a form, which runs when a commandbutton is pressed.

In
the
Sub an array is dimmed and filled with values.

e.g. Dim NANames(1 To 20, 0 To 2)

I need the values of the Array in another Sub, but the array is only
available in the specific Sub when created. Declaring it Public is

not
possible with an array, so I need another way of doing this.

Anyone ?

Thanks !












  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default How to keep an Array 'alive'

Water it regularly and make sure it gets plenty of sunlight.

(Sorry, I just couldn't resist ;)
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to keep an Array 'alive'

Fred wrote:
* Declaring it Public is not
possible with an array*


Why not, but you have to declare it in Module1 (Or whatever the mai
module is).

I have a similar problem, I have a public array which is used for
different userforms so there is no point filling the array twice (Whic
is 70x3) everytime I open a userform I filled it when opening th
workbook but for some reason when you press stop button on debugger an
some other times I have to keep running the procedure to fill it sinc
it is emptied for some reason.

Best Regards

Noo

--
Message posted from http://www.ExcelForum.com

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
Complex conditional summing - array COUNT works, array SUM gives#VALUE fatcatfan Excel Worksheet Functions 4 November 18th 09 06:41 PM
Prevent cell/array references from changing when altering/moving thecell/array nme Excel Discussion (Misc queries) 1 September 19th 08 01:53 PM
meaning of : IF(Switch; Average(array A, array B); array A) DXAT Excel Worksheet Functions 1 October 24th 06 06:11 PM
How to keep a Variable alive after a Call to Sub Routine Dennis Excel Discussion (Misc queries) 2 July 27th 05 10:57 PM
variant array containing cel adresses convert to actual ranges-array Peter[_21_] Excel Programming 5 December 10th 03 09:50 PM


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