Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Excel 2010 VBA - Should I initialize variables?

Posted on 2012-09-12
9
Medium Priority
?
435 Views
Last Modified: 2012-09-12
I'm an old school COBOL programmer and I'm used to initializing variables before using them.  Should I do that when programming in VBA?
0
Comment
Question by:brothertruffle880
  • 6
  • 3
9 Comments
 
LVL 50

Accepted Solution

by:
Martin Liss earned 2000 total points
ID: 38393073
There is no need to initialize variables that are defined in a Sub or a Function. VBA does it for you.
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38393076
BTW I did COBOL > 40 years ago.

I should also amend my first post to say that any version of VB does the same.
0
 
LVL 36

Expert Comment

by:Norie
ID: 38393083
It depends what type of variables they are.

Some like object variable you will need to initialize, others you won't but it wouldn't do any harm if you did.

Actually, some variables are kind of initialized anyway.

For example if you declare a variable as Integer it will have the initial value 0.

Declare as a string and a variable will have the initial value of "", an empty string.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 50

Expert Comment

by:Martin Liss
ID: 38393096
It depends what type of variables they are.

Some like object variable you will need to initialize, others you won't but it wouldn't do any harm if you did.

Actually, some variables are kind of initialized anyway.
Variables are variables and objects are objects. With objects you create new ones which is different than initialization. What do you mean by 'kind of"??
0
 
LVL 36

Expert Comment

by:Norie
ID: 38393112
Martin

Object variable - a variable that refers to an object.

By 'kind of' I mean that they aren't given a value, which might not be regarded as initalisation exactly.
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38393124
Okay I'm sure that's the official definition of an object but you don't say MyObject = 0 but rather Set MyObject = Whatever-objcet-type-it-is.

VB always gives a variable an initial value even if its 0 or "".
0
 
LVL 36

Expert Comment

by:Norie
ID: 38393138
Sorry I'm a bit lost.

I'm not suggesting using something like MyObject = 0.

I know you initialize object variables by setting them to reference and object.
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38393201
@brothertruffle880: Let me apologize for imnorie's and my discussion of objects which probably doesn't interest you.

The answer to your question is in my first post with some additional info in imnorie's first post describing what the variables are initialize to.
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38393269
Glad I was able to help.

Marty - MVP 2009 to 2012
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question