ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   set variable once (https://www.excelbanter.com/excel-programming/318487-set-variable-once.html)

mike allen[_2_]

set variable once
 
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



Frank Kabel

set variable once
 
Hi
try:
dim public user as range
set user = sheets("1").range("a1")

sub temp1()
msgbox user.value
end sub


--
Regards
Frank Kabel
Frankfurt, Germany

"mike allen" schrieb im Newsbeitrag
...
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




Bob Phillips[_6_]

set variable once
 
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)


"mike allen" wrote in message
...
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





mike allen[_2_]

set variable once
 
"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

"Frank Kabel" wrote in message
...
Hi
try:
dim public user as range
set user = sheets("1").range("a1")

sub temp1()
msgbox user.value
end sub


--
Regards
Frank Kabel
Frankfurt, Germany

"mike allen" schrieb im Newsbeitrag
...
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






Bob Phillips[_6_]

set variable once
 
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)


"mike allen" wrote in message
...
"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

"Frank Kabel" wrote in message
...
Hi
try:
dim public user as range
set user = sheets("1").range("a1")

sub temp1()
msgbox user.value
end sub


--
Regards
Frank Kabel
Frankfurt, Germany

"mike allen" schrieb im Newsbeitrag
...
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








Frank Kabel

set variable once
 
Hi Bob
thanks for the correction. Should not code directly in OE :-)

--
Regards
Frank Kabel
Frankfurt, Germany

"Bob Phillips" schrieb im
Newsbeitrag ...
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)


"mike allen" wrote in message
...
"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

"Frank Kabel" wrote in message
...
Hi
try:
dim public user as range
set user = sheets("1").range("a1")

sub temp1()
msgbox user.value
end sub


--
Regards
Frank Kabel
Frankfurt, Germany

"mike allen" schrieb im Newsbeitrag
...
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









Bob Phillips[_6_]

set variable once
 
I would have said that, but I might have got some flak back :-)

Bob


"Frank Kabel" wrote in message
...
Hi Bob
thanks for the correction. Should not code directly in OE :-)

--
Regards
Frank Kabel
Frankfurt, Germany

"Bob Phillips" schrieb im
Newsbeitrag ...
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)


"mike allen" wrote in message
...
"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

"Frank Kabel" wrote in message
...
Hi
try:
dim public user as range
set user = sheets("1").range("a1")

sub temp1()
msgbox user.value
end sub


--
Regards
Frank Kabel
Frankfurt, Germany

"mike allen" schrieb im Newsbeitrag
...
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











Frank Kabel

set variable once
 
Hi Bob
but only some minor shooting back <ebg

--
Regards
Frank Kabel
Frankfurt, Germany

"Bob Phillips" schrieb im
Newsbeitrag ...
I would have said that, but I might have got some flak back :-)

Bob


"Frank Kabel" wrote in message
...
Hi Bob
thanks for the correction. Should not code directly in OE :-)




All times are GMT +1. The time now is 06:09 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com