Public Variables

  • Thread starter Thread starter Richard
  • Start date Start date
R

Richard

Hi,

I understand the reason for declaring variables, but get fed up with
declaring them for each Sub in each module.

Can I declare my vaiables in 'This Workbook' using public.
Can I also 'publicly' declare what the vaiable refers to so that if I do the
following

Public TodaysDate As String

that I could use

TodaysDate = Worksheets("Dates").range("UpdateToday")
TodaysDate = Format(TodaysDate, "dd-mmm-yy")

in any module/sub.

Unfortunately I have to work with dates that are strings and others that are
dates in various formats.

Thanks for any help / advice

Richard
 
Hi
You can declare variables as public at the top of any code module &
they will be available to all modules.
If you want TodaysDate to be a date or a string, then declare it as a
Variant.

You might want to pause and rethink your strategy here though. When it
comes to managing a lot of code in several modules & you have some
debugging to do on a code rewrite you will dearly, dearly wish you
hadn't gone down this road...
More than that, I would put as much commentary as I could next to each
variable as it is declared to tell me exactly what it is for.

regards
Paul
 
You might want to pause and rethink your strategy here though. When it
comes to managing a lot of code in several modules & you have some
debugging to do on a code rewrite you will dearly, dearly wish you
hadn't gone down this road...

What is the rationale for this argument? I typically have dozens of modules
in my appplications. That is all part of modular/OO techniques.
 
Bob,

Think he is talking about declaring all ones variables as public in one
location - as suggested by the OP - not using multiple modules.

I would amend Paul's statement about declaring variables public in any
module to qualify any module to be any general/standard module (since the OP
specifically mentioned the Thisworkbook module as I recall)
 
Hi
I think the poster wants to declare a variable once and then use that
name in several modules, within several subs, and for it to mean
several different things. It was the variable scoping I suggested he
rethink - not the modularity.
regards
Paul
 

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