+ Reply to Thread
Results 1 to 7 of 7

variable scope within functions

  1. #1
    Registered User
    Join Date
    07-25-2008
    Location
    Tucson, AZ
    Posts
    90

    variable scope within functions

    I am having trouble with keeping Public variables within scope. The variables are declared as arguments into a function. Within that function, there is a call to another Subroutine. When the code progress into the Subroutine, the values of the variables are lost/reset rather than retained at their originally defined values.

    Can anyone tell me a way around this? besides combining all the code explicitly within the function

    Please Login or Register  to view this content.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: variable scope within fuinctions

    First, public variables are declared outside the scope of any procedure.

    Second, your static function doesn't show any declared variables.

    So ... I don't understand your question.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    07-25-2008
    Location
    Tucson, AZ
    Posts
    90

    Re: variable scope within functions

    well, I thought the call to the function WAS the variable declaration

    I can't define them inside the function since it would be a duplicate declaration

    I have also tried declaring them public outside (above) the beginning of the function, but the same problem occurs (values are lost)

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: variable scope within functions

    I am hampered by not having a clue what you're trying to do.

    Can you explain?

  5. #5
    Registered User
    Join Date
    07-25-2008
    Location
    Tucson, AZ
    Posts
    90

    Re: variable scope within functions

    my objectives are two-fold:

    1. define SINDAmodel as a Function, such that is can be used as a Worksheet function

    2. declare the input arguments to SINDAmodel as Public variables, such that they can be passed among other procedures while retaining their values

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: variable scope within functions

    First, only Functions (not Subs) can be invoked from the worksheet, and in that context, such functions are called UDFs (User-Defined ~).

    Second, no UDF can do anything (with some arcane exceptions) other than return a value to the cell in which it's entered. That restriction continues to any sub called by a UDF.

    You can have a UDF that returns values to the worksheet that were calculated by a Sub:
    Please Login or Register  to view this content.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: variable scope within functions

    I should add that if the variables don't need to be seen outside the module in which they are declared, replace Public with Dim

+ 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