Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Correct declaration of variables

Marko,

On the last point, no it is not necessary to set strings to "" at the end of
the procedure, trust VBA's garbage collection to sort that out.

There is a view as to whether to set objects to Nothing at the end. Take a
look at this previous post for some thoughts on this topic
http://tinyurl.com/sr3o


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Bob Phillips" wrote in message news:...
Marko,

It certainly does matter.

A variable declared outside of a function is available to every procedure

in
that module (and if it is declared as Public, it can even be accessed from
outside of the module).

A variable declared inside a function is only available to that function.

Consider this code

Dim var1 As Integer


Sub Test1()
Dim var1 As Integer

var1 = 17
MsgBox "Test1 returns - " & var1

End Sub

Sub Test2()

MsgBox "Test2 returns - " & var1

End Sub

Run Test1 and the Msgbox declares a value of 17. Then run Test2. The

MsgBox
returns 0, not 17.

This is what is happening. A module level variable called 'var1' is first
declared. When you run Test1, it declares a procedure level variable

called
'var1' and sets its value to 17. MsgBox displays this, but the variable is
cleared upon exiting Test1. Then when you run Test2, it picks up the

module
variable 'var1', which has not been set, so it displays the value 0.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"marko" <no@email wrote in message
...
Hi all,
Just a quick few Q's about good VBA programming practice.
When creating a vba function in a module that will be called repeatedly

by
a
macro, do the variable declarations go before the start of function or

is
it
better to declare them inside? Does it matter?
Are they created and destroyed everytime the function is run?
Is it good practice to set the variables to "" at the end of the

function
or
macro?

Marko






  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Correct declaration of variables

Thanks Bob

Marko

"Bob Phillips" wrote in message
...
Marko,

On the last point, no it is not necessary to set strings to "" at the end

of
the procedure, trust VBA's garbage collection to sort that out.

There is a view as to whether to set objects to Nothing at the end. Take a
look at this previous post for some thoughts on this topic
http://tinyurl.com/sr3o



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
Array Declaration. apis Excel Discussion (Misc queries) 1 November 5th 11 01:53 PM
Workbook Declaration Varne Excel Discussion (Misc queries) 3 April 29th 08 09:49 AM
VBA - variable declaration Jeff Excel Discussion (Misc queries) 3 January 9th 08 12:45 PM
Correct declaration of variables marko Excel Programming 1 November 30th 03 09:56 AM
Variable Declaration?? Tom Ogilvy Excel Programming 1 August 8th 03 06:45 PM


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