PDA

Click to See Complete Forum and Search --> : EXCEL: How to: Pass Global Variables from Window_Open event: Resolved


Webtest
May 24th, 2005, 11:07 AM
Esteemed Forum Participants and Lurkers:
===============================

Thanks to Savelinus for helping me get started, but I didn't get very far. Here is the code I have so far, but I get an error when I try to reference the variable I was trying to make Global:Option Explicit
'Module: ThisWorkbook(Code)
'
Public G_STR As String 'Define the Global Variable
'
Private Sub Workbook_Open() 'The EVENT for Opening the Workbook
Dim astr As String

' Show that the Workbook has opened
astr = ActiveSheet.Name
MsgBox ("Hello, World! Sheet " & astr & " is OPEN!")
' Show that the Variable has been defined - at least locally
G_STR = "THIS IS A GLOBAL TEST STRING"
MsgBox (G_STR)

End SubOption Explicit
'Module: Module1(Code)
'
'MACRO to test Initialized Global Variables - View the initialized Global
Sub G_Test()
MsgBox (G_STR) '<<<< Compile error: Variable not defined
End SubSo if I an going to use the Workbook_Open event to initialize some global variables, how do I get it to work in other modules?

Thank you for your gracious comments, suggestions, and assistance.

RobDog888
May 24th, 2005, 11:16 AM
Place Public G_STR As String in the module - Module1 in the general declarations section.
Option Explicit
'Module: ThisWorkbook(Code)
'
Private Sub Workbook_Open() 'The EVENT for Opening the Workbook
Dim astr As String

' Show that the Workbook has opened
astr = ActiveSheet.Name
MsgBox ("Hello, World! Sheet " & astr & " is OPEN!")
' Show that the Variable has been defined - at least locally
G_STR = "THIS IS A GLOBAL TEST STRING"
Call G_Test 'Changed to your sub

End Sub

Option Explicit
'Module: Module1(Code)

Public G_STR As String

'MACRO to test Initialized Global Variables - View the initialized Global
Sub G_Test()
MsgBox (G_STR)
End SubRemember that "ThisWorkbook" is a class and not a standard module. ;)

Webtest
May 24th, 2005, 11:56 AM
Hey, RobDog ...

That works! Thank you very much! There is WAY too much to remember already ... I don't know if I can cram any more into this feeble old brain, but I'll try.

RobDog888
May 24th, 2005, 01:00 PM
Your welcome. :)

Just imagine, I think like that all the time I learn something new. It never ceases to amaze me of the complexity of computers.
Just when you think your getting a handle on things M$ goes and creates something new. :(