Results 1 to 8 of 8

Thread: [RESOLVED] Decalring global variables in VBA

  1. #1

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    46

    Resolved [RESOLVED] Decalring global variables in VBA

    Hi, I am quite new to VBA and i wan to know how can i declare a global variable in VBA. I understand tat i need to put this definition in a module, something like header files in C.. but what else is require in the module?

    can someone pls help me by giving me exactly the codes for declaring a golbal variable in a module?

    Global Variable : toolPath
    Type : String

    so do i define as Public toolPath As String?? anything esle n where to place it? thks

    btw, i am using VBA in access... so the "lifespan" of the variable will last till the entire application is closed right?

    thks
    Last edited by fulltime; May 26th, 2006 at 01:25 AM.

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Decalring global variables in VBA

    You have yourself answered your question.

    You add a module in your VBA project and then write
    VB Code:
    1. Public toolPath As String
    This makes the variable available to all other modules.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    46

    Re: Decalring global variables in VBA

    Quote Originally Posted by Shuja Ali
    You have yourself answered your question.

    You add a module in your VBA project and then write
    VB Code:
    1. Public toolPath As String
    This makes the variable available to all other modules.
    thks.. wats the difference between a module and a class module?

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

    Re: Decalring global variables in VBA

    Moved
    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

  5. #5

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    46

    Re: Decalring global variables in VBA

    wat if i want a function to return a value? how can i declare it?

    Eg Function Name =

    Private Sub Test()

    i want it to return a string. how can i do it? thks...

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

    Re: Decalring global variables in VBA

    A Sub does not return anything at all. A Function does.
    VB Code:
    1. 'In a standard Module:
    2. Option Explicit
    3.  
    4. Public Function AnimalSound() As String
    5.     AnimalSound = "Meow.NET"
    6. End Function
    VB Code:
    1. 'Usage:
    2. Private Sub Command1_Click()
    3.     MsgBox AnimalSound ' "Meow.NET"
    4. End Sub
    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

  7. #7
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Decalring global variables in VBA

    Class Module : It is basically a template using which objects can be created. The code that is written in the class module basically describes the attributes (methods, events, properties, fields) of the object that are created using that class module. Class modules are the foundation of OOP in VB.

    Module: MOdules are basically the files where you write common procedures and declare public data that will be accessed throughout the application.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  8. #8

    Thread Starter
    Member
    Join Date
    May 2006
    Posts
    46

    Re: Decalring global variables in VBA

    got it.. thks to the both of u

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