set variable once

  • Thread starter Thread starter mike allen
  • Start date Start date
M

mike allen

is it possible to set a variable just once and have many subs use it?
example:

user = sheets("1").range("a1") 'i would like to have this set only once at
top of module sheet

sub temp1()
msgbox user 'i would like for this and many other subs to know what 'user'
is
end sub

i know i can define 'user' in a sub, then use sub temp1(user), but i would
rather not since there will be so many subs w/ 'user' needed. thanks, mike
allen
 
Hi
try:
dim public user as range
set user = sheets("1").range("a1")

sub temp1()
msgbox user.value
end sub
 
Make it a Public variable, that is declare it at the top of the module,
before any macros, and set it in the first called macro.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
"expected: identifier" comes up on "public" in first line. the entire
first line is red, probably pre-warning me of this error. i copied exactly
as you have. i must be doing something else wrong. thanks, mike allen
 
public user as range

sub temp1()
set user = sheets("1").range("a1")
msgbox user.value
end sub

You don't use Dim and Public, and you cannot use Set method outside of a
procedure.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Hi Bob
thanks for the correction. Should not code directly in OE :-)
 
I would have said that, but I might have got some flak back :-)

Bob
 

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

Back
Top