+ Reply to Thread
Results 1 to 5 of 5

proper format for functions

  1. #1
    Forum Contributor
    Join Date
    09-19-2004
    Posts
    252

    proper format for functions

    I've been searching the net and can't find a proper format for using functions within the personal workbook. Can I put a function on one module and then recall it in another as long as they are in the same workbook??? If so, what is the proper format for that so that everything functions properly??

  2. #2
    Forum Contributor
    Join Date
    11-16-2004
    Posts
    282

    Understanding Scope and Visibility

    From Microsoft Visual Basic Help Topic - Understanding Scope and Visibility


    Scope refers to the availability of a variable, constant, or procedure for use by another procedure. There are three scoping levels: procedure-level, private module-level, and public module-level.

    You determine the scope of a variable when you declare it. It's a good idea to declare all variables explicitly to avoid naming-conflict errors between variables with different scopes.

    Defining Procedure-Level Scope
    A variable or constant defined within a procedure is not visible outside that procedure. Only the procedure that contains the variable declaration can use it. In the following example, the first procedure displays a message box that contains a string. The second procedure displays a blank message box because the variable strMsg is local to the first procedure.

    Sub LocalVariable()
    Dim strMsg As String
    strMsg = "This variable can't be used outside this procedure."
    MsgBox strMsg
    End Sub

    Sub OutsideScope()
    MsgBox strMsg
    End Sub

    Defining Private Module-Level Scope
    You can define module-level variables and constants in the Declarations section of a module. Module-level variables can be either public or private. Public variables are available to all procedures in all modules in a project; private variables are available only to procedures in that module. By default, variables declared with the Dim statement in the Declarations section are scoped as private. However, by preceding the variable with the Private keyword, the scope is obvious in your code.

    In the following example, the string variable strMsg is available to any procedures defined in the module. When the second procedure is called, it displays the contents of the string variable strMsg in a dialog box.

    ' Add following to Declarations section of module.
    Private strMsg sAs String

    Sub InitializePrivateVariable()
    strMsg = "This variable can't be used outside this module."
    End Sub

    Sub UsePrivateVariable()
    MsgBox strMsg
    End Sub

    Note Public procedures in a standard module or class module are available to any referencing project. To limit the scope of all procedures in a module to the current project, add an Option Private Module statement to the Declarations section of the module. Public variables and procedures will still be available to other procedures in the current project, but not to referencing projects.

    Defining Public Module-Level Scope
    If you declare a module-level variable as public, it's available to all procedures in the project. In the following example, the string variable strMsg can be used by any procedure in any module in the project.

    ' Include in Declarations section of module.
    Public strMsg As String

    All procedures are public by default, except for event procedures. When Visual Basic creates an event procedure, the Private keyword is automatically inserted before the procedure declaration. For all other procedures, you must explicitly declare the procedure with the Private keyword if you do not want it to be public.

    You can use public procedures, variables, and constants defined in standard modules or class modules from referencing projects. However, you must first set a reference to the project in which they are defined.

    Public procedures, variables, and constants defined in other than standard or class modules, such as form modules or report modules, are not available to referencing projects, because these modules are private to the project in which they reside.
    Hope this helps,
    theDude

  3. #3
    Forum Contributor
    Join Date
    09-19-2004
    Posts
    252
    I'm still a little confused. What I'm trying to do is put a module in my personal workbook that sends an email to certain email addresses.

    Please Login or Register  to view this content.
    but have several different other modules that access this but each of the other modules will bring the subject and body into the email module. I can't figure out how to start with the one module and have it jump over to the email module while carrying the values to the subject and body variables.

  4. #4
    Forum Contributor
    Join Date
    11-16-2004
    Posts
    282

    Calling macros from other macros and passing variables

    You can define standard macro procedures to accept variables from other macro procedures and call the standard procedure from the other procedures. To test the concept, copy the following sample code (the 'Standard' one) into a module:
    Please Login or Register  to view this content.
    Copy these two macros into a different module and run them. They pass two variables required to the macro above:
    Please Login or Register  to view this content.
    Notice how the two macros above use the same variable names (eSubject & eBody), but the values are different depending on the macro that runs.

    So, you need to add the required variables to your e-mail macro definition Sub sendEMail(eSubject As String, eBody As String), and then replace your code:
    Please Login or Register  to view this content.
    with this code:
    Please Login or Register  to view this content.
    Hope this helps,
    theDude
    Last edited by theDude; 02-12-2007 at 08:30 PM.

  5. #5
    Forum Contributor
    Join Date
    09-19-2004
    Posts
    252
    That's awesome! Thanks theDude!

+ 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