ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using a variable in more than one sub procedure (https://www.excelbanter.com/excel-programming/379413-using-variable-more-than-one-sub-procedure.html)

keri

Using a variable in more than one sub procedure
 
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?


Kent

Using a variable in more than one sub procedure
 
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

Using a variable in more than one sub procedure
 
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

Using a variable in more than one sub procedure
 
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

Using a variable in more than one sub procedure
 
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

Using a variable in more than one sub procedure
 
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

keri

Using a variable in more than one sub procedure
 
Thanks.



All times are GMT +1. The time now is 12:40 PM.

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