View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Public Variables

First, if you want to share that variable between modules, you'll want to make
it Public (not just use Dim).

Public strYear as Variable 'sometimes it's a date
Public VarsAreInitialized as Boolean 'more on this later

This would be at the top of the module.

Then you need something that does the actual work.

I'd suggest a Subroutine that initializes all your variables (and you'd use that
public variable to check).

In any old procedure that may use any of the public variables.

if varsareinitialized = false then
call InitializeTheVariables
end if
msgbox strYear

========
Then in a new procedure, initialize all the variables you need.

Sub InitializeTheVariables()
VarsAreInitialized = true 'the flag that keeps track

'your code for setting strYear
strYear = WorksheetFunction.WorkDay(Now(), -1)
strYear = Format(strYear, "yyyy")

'or dim strYear as a String and do it all at once:
strYear = format(WorksheetFunction.WorkDay(Now(), -1), "yyyy")

End Sub






Richard wrote:

Hi

Instead of restating the following in each module or procedure I would like
to set a public variable to do the following:

Dim strYear As String

strYear = WorksheetFunction.WorkDay(Now(), -1)
strYear = Format(strYear, "yyyy")

I understand that in a seperate module I can simply state:

public strYear As String

but do not understand how to state that strYear is equal to
WorksheetFunction.WorkDay(Now(), -1) and to format it correctly.

Can you please advise.

Thanks in advance

Richard


--

Dave Peterson