Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default 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?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default 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?


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default 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?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default 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?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default 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?



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,886
Default 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
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default Using a variable in more than one sub procedure

Thanks.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Procedure name as variable Redbeard Excel Programming 15 May 2nd 06 11:17 AM
Variable procedure call Art Excel Programming 2 December 16th 05 01:16 PM
Call procedure using variable donbowyer Excel Programming 2 October 28th 05 09:21 AM
Public/Procedure Variable Otto Moehrbach[_6_] Excel Programming 2 February 6th 04 04:58 PM
can I call a procedure using a variable Santiago Gomez Excel Programming 8 December 24th 03 09:08 PM


All times are GMT +1. The time now is 07:27 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"