Results 1 to 3 of 3
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing a Private Variable (XL97;SR2)

    I have a userform that declares a private variable and would like to pass this variable into a module. I have successfully passed the private variable into a module function but can not pass it to a sub. At the beginning of the module code I have stated "Public strMyPrivateVariable as String. Any Ideas as to why it can not be passed to the sub?

    John

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Passing a Private Variable (XL97;SR2)

    Private variables cannot be used by code in other modules.

    Cleanest is to use the variable in a call to a Sub or Function that accepts a corresponding variable as an argument.

    Another way is to use Property Get/Let.

    Another way is to declare the variable to be Public outside of the Userform.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Passing a Private Variable (XL97;SR2)

    You can add a parameter to a Sub just as you do for a Function, that is:

    Sub MySub(strMyString as String)

    Of course, this may have unintended consequences if you have called this procedure in other places or want to call it from the Macros... dialog.

    The public variable you declared in the module is available in the Sub. Have you tried setting the public (global) variable from inside your form procedures? People tell me it's bad form, but if you don't want to add an argument to your Sub, it's an alternate way to get the variable into its scope.

Posting Permissions

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