+ Reply to Thread
Results 1 to 4 of 4

Can a variable be used across different Sub Functions?

Hybrid View

  1. #1
    Registered User
    Join Date
    07-18-2006
    Posts
    73

    Can a variable be used across different Sub Functions?

    I have a macro setup to run different reports in Excel. I have the filename picked up from cell A1 in one set of code, I also want it to be picked up and used in another Sub function. It is stored in a different module. Is there a way I can declare it so I can use it in both?

    I read something about declaring it globally, would that work?

    Thanks,

    ~J

  2. #2
    Registered User
    Join Date
    02-22-2006
    Posts
    28

    Can a variable be used across different Sub Functions?

    Hi,

    1)

    Declare a public variable of appropriate data type in any one onf the modules. And you can use that in all the modules.

    Eg: In module1.bas declare

    public gIntEmpNumber as Integer

    you can access this variable in module2.bas or another module.

    2)
    Declare a parameter in the other sub function and pass the filepath value and use that in the sub function

    Best Wishes
    mtpSuresh

  3. #3
    Registered User
    Join Date
    07-18-2006
    Posts
    73
    Sorry just a tad confused. So in one module I would set

    Public Filename as String

    Then I just use Filenema whenever in any module?

    Thanks,

    ~J

  4. #4
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    Yes, that's the way.

    If you wish to use just one (general) module, you could also declare the variable globally using Dim filename as String at the top of the module, just before the first Sub. "Filename" will then become accessible to all subroutines in the module. The moral in the message is that you don't need to have multiple modules (when one will suffice) on the strength of the service provided by PUBLIC declarations.
    HTH
    Myles

    ...constantly looking for the smoother pebble while the whole ocean of truth lies before me.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1