Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

declaring a variable

Status
Not open for further replies.

bencnu

Technical User
Nov 3, 2003
34
US
I have only programed in c++, so I know this may sound like a dumb question, but how and where do you declare a local or global variable?

THANKS
 
Hi
I've never been a complete genius at this but...

Local variable - available only to the routine

Sub Proc()
Dim var as Range
'do stuff
End Sub

Module (or Private) variables are declared outside the subs but within the module using either the Dim or Priavate statements

Dim var as integer
Sub Proc()
var = 1
End Sub

Sub Proc2()
var = 100
End Sub

Public as above but variable is available to all modules in a project. Can also be used in class modules but that's definately beyond me! COM and such things!!

Check out the help files for Dim, Private, Friend & Public keywords, maybe also Static & Const Data types will probably be roughly the same as with C++ (integer, long, double, boolean etc

You should also look into "Option Explicit" I don't know C++ but I am aware that VB/VBA is quite forgiving in ways that can lead to problems, ie you don't actually have to declare variables. Option Explicit will force variable declaration. It sits at the top of each module and can be set thru options or something but I can't remember the exact route to it.

Hope this helps
;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
The faqs ma'am, just the faqs. Get the best from these forums : faq222-2244
 
Thank you thats what i was looking for :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top