Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Help me understanding variable scope

I create a public array defined on top of the macro to be global in 1 of the
worksheet. The array contains a range of a worksheet. I have a procedure
called by a checkbox_click routine to see if a readflag is true to skip
loading the range into the global array so that it doesn't keep reading every
single time. My problem is that once the procedure is ran by the click
routine, the global array content disappears. What can I do to keep the value
intact once the array is loaded? Another thought is to verify if the global
array has any values in it but since I define the array as variant, it
doesn't exist when it's not loaded. Is there a function in VBA that allows me
to check if an array is defined?

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Help me understanding variable scope

I put my public variables in a General module--not behind a worksheet.

And maybe you could use:

if isarray(myVar) then
'hey it's an array!
else
'not an array
end if



matelot wrote:

I create a public array defined on top of the macro to be global in 1 of the
worksheet. The array contains a range of a worksheet. I have a procedure
called by a checkbox_click routine to see if a readflag is true to skip
loading the range into the global array so that it doesn't keep reading every
single time. My problem is that once the procedure is ran by the click
routine, the global array content disappears. What can I do to keep the value
intact once the array is loaded? Another thought is to verify if the global
array has any values in it but since I define the array as variant, it
doesn't exist when it's not loaded. Is there a function in VBA that allows me
to check if an array is defined?

Thanks


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Help me understanding variable scope

As well as Dave's suggestion, are you using "End" anywhere in your code ?
That will clear variables.

By the way, one quick/dirty way to test if an array has been initialised is.
It has a few minor drawbacks, that you can research if necessary.

Private Sub CommandButton2_Click()
Dim arr() As Long

Debug.Print "Is initilaised: " & CBool(Not (Not arr))

ReDim arr(0)
Debug.Print "Is initilaised: " & CBool(Not (Not arr))

End Sub

NickHK

"matelot" wrote in message
...
I create a public array defined on top of the macro to be global in 1 of

the
worksheet. The array contains a range of a worksheet. I have a procedure
called by a checkbox_click routine to see if a readflag is true to skip
loading the range into the global array so that it doesn't keep reading

every
single time. My problem is that once the procedure is ran by the click
routine, the global array content disappears. What can I do to keep the

value
intact once the array is loaded? Another thought is to verify if the

global
array has any values in it but since I define the array as variant, it
doesn't exist when it's not loaded. Is there a function in VBA that allows

me
to check if an array is defined?

Thanks



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Help me understanding variable scope

Thanks for the suggestion from both of you. I end up using Dave's solution.
It's simple enough to implement.

Thanks again to both of you.

"NickHK" wrote:

As well as Dave's suggestion, are you using "End" anywhere in your code ?
That will clear variables.

By the way, one quick/dirty way to test if an array has been initialised is.
It has a few minor drawbacks, that you can research if necessary.

Private Sub CommandButton2_Click()
Dim arr() As Long

Debug.Print "Is initilaised: " & CBool(Not (Not arr))

ReDim arr(0)
Debug.Print "Is initilaised: " & CBool(Not (Not arr))

End Sub

NickHK

"matelot" wrote in message
...
I create a public array defined on top of the macro to be global in 1 of

the
worksheet. The array contains a range of a worksheet. I have a procedure
called by a checkbox_click routine to see if a readflag is true to skip
loading the range into the global array so that it doesn't keep reading

every
single time. My problem is that once the procedure is ran by the click
routine, the global array content disappears. What can I do to keep the

value
intact once the array is loaded? Another thought is to verify if the

global
array has any values in it but since I define the array as variant, it
doesn't exist when it's not loaded. Is there a function in VBA that allows

me
to check if an array is defined?

Thanks




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Help me understanding variable scope

For the record, one of the draw backs is trying to use this construction
with a custom type. e.g
Private type Point
X as single
Y as single
end type

With an array of this Type, I would get itermittent error #16 "Expression
too complex" at seemingly random/unrelated places in code.
Use proper methods of testing for an initialised array, or waste a few hours
trying to debug...<g

NickHK

"NickHK" wrote in message
...
As well as Dave's suggestion, are you using "End" anywhere in your code ?
That will clear variables.

By the way, one quick/dirty way to test if an array has been initialised

is.
It has a few minor drawbacks, that you can research if necessary.

Private Sub CommandButton2_Click()
Dim arr() As Long

Debug.Print "Is initilaised: " & CBool(Not (Not arr))

ReDim arr(0)
Debug.Print "Is initilaised: " & CBool(Not (Not arr))

End Sub

NickHK

"matelot" wrote in message
...
I create a public array defined on top of the macro to be global in 1 of

the
worksheet. The array contains a range of a worksheet. I have a procedure
called by a checkbox_click routine to see if a readflag is true to skip
loading the range into the global array so that it doesn't keep reading

every
single time. My problem is that once the procedure is ran by the click
routine, the global array content disappears. What can I do to keep the

value
intact once the array is loaded? Another thought is to verify if the

global
array has any values in it but since I define the array as variant, it
doesn't exist when it's not loaded. Is there a function in VBA that

allows
me
to check if an array is defined?

Thanks





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
Scope of variable avi Excel Programming 2 April 12th 07 09:02 AM
UNDERSTANDING VARIABLE SCOPE -JEFF-[_2_] Excel Programming 9 August 24th 05 10:25 PM
variable scope mgasparel Excel Programming 2 February 16th 05 05:22 PM
Variable Scope Kevin Excel Programming 1 September 14th 04 07:05 PM
Variable scope TonyM Excel Programming 5 April 24th 04 01:02 PM


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