[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.excel.programming

Using a variable in more than one sub procedure

keri

12/14/2006 1:08:00 PM

I currently have this is a sub and parts of the sub use the answer.
This sub will run first.

Dim answer as integer
answer = inputbox(How many sheets?)

However i would like to use the answer again in another sub that runs
later, without the input box again. Is this possible?

6 Answers

Kent

12/14/2006 1:26:00 PM

0

If the sub is on the same module/sheet/userform then you should just
make a global variable for it: e.g. "Dim glDescriberAnwer as long" at
the top of the form.

When the other sub(s) wants to use it, just be sure the code is smart
enough to know the un-initialized variable (i.e. 0) isn't what the user
inputted. If necessary, use a separate (boolean) global variable to
tell if the variable has been initialized or not.



keri wrote:
> I currently have this is a sub and parts of the sub use the answer.
> This sub will run first.
>
> Dim answer as integer
> answer = inputbox(How many sheets?)
>
> However i would like to use the answer again in another sub that runs
> later, without the input box again. Is this possible?

keri

12/14/2006 1:40:00 PM

0

Sorry i'm new to Excel and my knowledge is tiny.

I'm guessing you mean to declare the variable where it says Option
Explicit.
Does the gl refer to it being a global variable?

How and where would I put the othere variable to show true/false if it
has been answered already? And once I had this second variable would I
have to refer to it with an If statement,

eg currently my second use of answer says;

For p = 1 to answer

Would it have to be
For p = 1 to answer if bla is true?

How would I word this?

keri

12/14/2006 1:40:00 PM

0

Sorry i'm new to Excel and my knowledge is tiny.

I'm guessing you mean to declare the variable where it says Option
Explicit.
Does the gl refer to it being a global variable?

How and where would I put the othere variable to show true/false if it
has been answered already? And once I had this second variable would I
have to refer to it with an If statement,

eg currently my second use of answer says;

For p = 1 to answer

Would it have to be
For p = 1 to answer if bla is true?

How would I word this?

keri

12/14/2006 1:41:00 PM

0

Sorry i'm new to Excel and my knowledge is tiny.

I'm guessing you mean to declare the variable where it says Option
Explicit.
Does the gl refer to it being a global variable?

How and where would I put the othere variable to show true/false if it
has been answered already? And once I had this second variable would I
have to refer to it with an If statement,

eg currently my second use of answer says;

For p = 1 to answer

Would it have to be
For p = 1 to answer if bla is true?

How would I word this?

Roger Govier

12/14/2006 2:14:00 PM

0

Hi Keri

Yes, declare the variable after Option Explicit and before the first of
your Subs.
The gl doesn't make it global it is just a convention used by many to
show that this is a global variable utilised by many routines.

In your case, Answer is the number of pages, and if the question hasn't
been asked, then the value will be 0 from the original dim statement so
I don't think you need to worry about setting a Boolean as to whether
you have answered.
Just change your loops to

If answer >0 Then
For p =1 to answer
. rest of your code here
.
End If

--
Regards

Roger Govier


"keri" <keri.dowson@diageo.com> wrote in message
news:1166103670.589282.64140@16g2000cwy.googlegroups.com...
> Sorry i'm new to Excel and my knowledge is tiny.
>
> I'm guessing you mean to declare the variable where it says Option
> Explicit.
> Does the gl refer to it being a global variable?
>
> How and where would I put the othere variable to show true/false if it
> has been answered already? And once I had this second variable would I
> have to refer to it with an If statement,
>
> eg currently my second use of answer says;
>
> For p = 1 to answer
>
> Would it have to be
> For p = 1 to answer if bla is true?
>
> How would I word this?
>


keri

12/14/2006 2:37:00 PM

0

Thanks.