Results 1 to 4 of 4

Thread: EXCEL: How to: Pass Global Variables from Window_Open event: Resolved

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Resolved EXCEL: How to: Pass Global Variables from Window_Open event: Resolved

    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:
    Code:
    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 Sub
    Code:
    Option 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 Sub
    So 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.
    Last edited by Webtest; May 24th, 2005 at 11:57 AM. Reason: [RESOLVED]
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: EXCEL: How to: Pass Global Variables from Window_Open event ???

    Place Public G_STR As String in the module - Module1 in the general declarations section.
    VB Code:
    1. Option Explicit
    2. 'Module:  ThisWorkbook(Code)
    3. '
    4. Private Sub Workbook_Open()    'The EVENT for Opening the Workbook
    5.     Dim astr As String
    6.    
    7.     ' Show that the Workbook has opened
    8.     astr = ActiveSheet.Name
    9.     MsgBox ("Hello, World! Sheet " & astr & " is OPEN!")
    10.     ' Show that the Variable has been defined - at least locally
    11.     G_STR = "THIS IS A GLOBAL TEST STRING"
    12.     Call G_Test 'Changed to your sub
    13.    
    14. End Sub
    15.  
    16. Option Explicit
    17. 'Module:  Module1(Code)
    18.  
    19. Public G_STR As String
    20.  
    21. 'MACRO to test Initialized Global Variables - View the initialized Global
    22. Sub G_Test()
    23.   MsgBox (G_STR)
    24. End Sub
    Remember that "ThisWorkbook" is a class and not a standard module.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: EXCEL: How to: Pass Global Variables from Window_Open event ???

    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.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: EXCEL: How to: Pass Global Variables from Window_Open event: Resolved

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width