Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 709
Default Public Variables

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Public Variables

You make your public declaration at the top of the module, outside any
procedure. Then you only have to assign the value and formatting once
inside any single procedure and it will apply wherever it is used in any
other procedure in that module.



"Richard" wrote in message
...
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Public Variables

Ps. You don't need the parens near the now() in your code. That's required in
a formula in a cell in excel.

This will work fine:
strYear = format(WorksheetFunction.WorkDay(Now, -1), "yyyy")
as will:
strYear = format(WorksheetFunction.WorkDay(Date, -1), "yyyy")

<<snipped
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 342
Default Public Variables

A brief summary, I hope it is not too cryptic:

€˘ Variable scope:
o Procedural level variable (declared within the procedure and available
only within the procedure).
o Module level variable (declared at the top of the module)
ď‚§Dim VariableName as Type (Available only within the module).
ď‚§Private VariableName as Type (Available only within the module).
ď‚§Public VariableName as Type (Available throughout the project).
€˘ Variable lifetime:
o By default variables retain their assigned value only until the
subroutine in which they are declared executes the exit or end statement.
o If the subroutine or the variable is declared as static the variable
retains its value until the workbook is closed.
o Module level variables are static.

Tom


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 709
Default Public Variables

Dave

thanks for the help that sorted it nicely. I am also grateful for the
simplified code to format the string I am creating.

Richard

"Dave Peterson" wrote:

Ps. You don't need the parens near the now() in your code. That's required in
a formula in a cell in excel.

This will work fine:
strYear = format(WorksheetFunction.WorkDay(Now, -1), "yyyy")
as will:
strYear = format(WorksheetFunction.WorkDay(Date, -1), "yyyy")

<<snipped
.

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
public variables johnny Excel Discussion (Misc queries) 7 February 27th 08 03:44 PM
Public variables johnny Excel Discussion (Misc queries) 2 February 24th 08 05:05 AM
Public Variables Jerry McNabb Excel Discussion (Misc queries) 0 February 24th 08 01:26 AM
Public Variables ExcelMonkey[_190_] Excel Programming 5 February 21st 05 10:12 AM
Public Variables Les Gordon Excel Programming 2 November 11th 04 12:29 PM


All times are GMT +1. The time now is 06:20 PM.

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

About Us

"It's about Microsoft Excel"