Not sure why it doesn't work for you.

I did this:

In a module I called "Declarations", I added:


Public Variable_Public
Global Variable_Global

Then, in the ThisWorkbook module,
I created:

Private Sub Workbook_Open()
    Variable_Public = "TestString_" & Format(Now(), "m-d-yy")
    Variable_Global = "TestString_" & Format(Now(), "m-d-yy")
End Sub

So, both the Global and Public variables are beins established
when I open the Workbook.

I then created a module and created the sub:


Sub TestVals()
    MsgBox "Variable_Public: " & Variable_Public & Chr(13) & "Variable_Global: 
" & Variable_Global
End Sub

I exited and saved the workbook.

When I opened the workbook, I executed the TestVals sub and it showed me the 
proper values.

So, clearly I've demonstrated that I can declare the variable
as either Public or Global
and modify the value in separate subs in different modules.

Now.. question:
Are you "declaring" the variable in another sub?

For instance:
If you say:


Public IDOSheet
Sub Test1()
    IDOSheet = "Init " & Format(Now(), "mm-dd-yyyy")
    Debug.Print "Test1a: IDOSheet = " & IDOSheet
    Test2
    Debug.Print "Test1b: IDOSheet = " & IDOSheet
    Test3
    Debug.Print "Test1c: IDOSheet = " & IDOSheet
End Sub
Sub Test2()
    Dim IDOSheet
    Debug.Print "Test2a: IDOSheet = " & IDOSheet
    IDOSheet = "Init2 " & Format(Now(), "mm-dd-yyyy")
    Debug.Print "Test2a: IDOSheet = " & IDOSheet
End Sub
Sub Test3()
    IDOSheet = "Init2 " & Format(Now(), "mm-dd-yyyy")
    Debug.Print "Test3a: IDOSheet = " & IDOSheet
End Sub

Your "Immediate" window will show:
==================================
Test1a: IDOSheet = Init 09-18-2009
Test2a: IDOSheet = 
Test2a: IDOSheet = Init2 09-18-2009
Test1b: IDOSheet = Init 09-18-2009
Test3a: IDOSheet = Init2 09-18-2009
Test1c: IDOSheet = Init2 09-18-2009
====================================

Which shows that since you use the "Dim" statement in Test2,
it is being declared as a "local" variable as well as a "Global" or "Public" 
variable.
Within the Test2 sub, it is as if it is a totally separate variable, because it 
IS!


What version of Excel are you using?
I THINK there was an earlier version of Excel in which the compiler did not
produce an ERROR if a variable was declared as "Public" or "Global" within a 
sub.

If you want the variable available to other subs, you have to EITHER declare
it outside of a sub, or pass it as a passed variable.

Come to think of it.. if you did something like:

Public IDOSheet
Sub Test1()
    IDOSheet = "Init " & Format(Now(), "mm-dd-yyyy")
    stat = Test2(IDOSheet)
End Sub
Function Test2(IDOSheet)

  'Testing
End Function

the use of (IDOSheet) in the Test2 function is effectively
declaring it as a "local" variable within Test2..

hope this gives you some food for thought...
without being totally out of line!

Paul

    



________________________________
From: Doug <dsrmccl...@gmail.com>
To: MS EXCEL AND VBA MACROS <excel-macros@googlegroups.com>
Sent: Friday, September 18, 2009 12:06:35 PM
Subject: $$Excel-Macros$$ Constant for a day



Each day my workbook creates a new worksheet and names it with the
following format:
"Init " + Format(Now(), "m-d-yy")  --ex. "Init 9-18-09"

I've declared a public variable IDOSheet as String (in one of the
modules) to use throughout the workbook. It works but it seems that I
keep having to assigning IDOSheet = "Init " + Format(Now(), "m-d-yy")
in any subs that use it.

There's got to be a better way to handle this. Is there a way to
declare this as a constant that will refresh each day?

I tried declaring it as: Public Const IDOSheet As String = "Init " +
Format(Now(), "m-d-yy")
but I'm getting the error message: Constant expression required

I'm guessing that Now() may be the problem.

As always, the help here is greatly appreciated.

Thanks.
Doug


--~--~---------~--~----~------------~-------~--~----~
----------------------------------------------------------------------------------
Some important links for excel users:
1. Excel and VBA Tutorials(Video and Text), Free add-ins downloads at 
http://www.excelitems.com
2. Excel tutorials at http://www.excel-macros.blogspot.com
3. Learn VBA Macros at http://www.vbamacros.blogspot.com
4. Excel Tips and Tricks at http://exceldailytip.blogspot.com
 

To post to this group, send email to excel-macros@googlegroups.com
If you find any spam message in the group, please send an email to:
Ayush Jain  @ jainayus...@gmail.com or
Ashish Jain @ 26may.1...@gmail.com
<><><><><><><><><><><><><><><><><><><><><><>
HELP US GROW !!

We reach over 6,500 subscribers worldwide and receive many nice notes about the 
learning and support from the group. Our goal is to have 10,000 subscribers by 
the end of 2009. Let friends and co-workers know they can subscribe to group at 
http://groups.google.com/group/excel-macros/subscribe
-~----------~----~----~----~------~----~------~--~---

Reply via email to