Variable Initialization

  • Thread starter Thread starter asit.agrawal
  • Start date Start date
A

asit.agrawal

Hi,

What is the procedure to initliaze a variable as soon as the a macro
is invoked. The variable and its value, thus initialized, needs to be
accessible to other macros and procedures being executed in sequence
thereafter.

Thanks.
 
There are 2 ways to make variable accessable to other procedure. One is to
declare the variable globally. While this seems like a good way to go you can
end up with a project that is very difficult to maintain. A better way to
make a variable accessabel to another procedure is to pass it to that
procedure.

1 - Global Example
dim str as string

sub MainStuff()
str = "this"
call OtherStuff
end sub

sub OtherStuff
str = str & " and that"
msgbox str
end sub

2 - Passing Example


sub MainStuff()
dim str as string
str = "this"
call OtherStuff(str)
end sub

sub OtherStuff(byval str as string)
str = str & " and that"
msgbox str
end sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Workbook Variables 2
VARIABLES AGAIN 3
Declaring Variables 3
Variables initiation 6
Public variable 7
Transfer variable between macros 4
Defining variables 14
Run-time 91 0

Back
Top