Consulting

Results 1 to 5 of 5

Thread: preserve variable value

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location

    preserve variable value

    I set :
    private sub command_button click()
    [VBA]dim doc as document
    set doc = documents.add
    [/VBA]

    end sub

    How can I preserve variable doc-value so that in other subs will be recognizable
    (lets say doc is nov document1 and when sub ends it should stay doc = document1)

    Thnx

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    This is an issue of "scope."
    Scope describes the "visibility" of a variable. If you make your variable declaration inside a routine (as you have in this case) it is only available inside that routine.

    You can raise the scope to module level by making the declaration at the start of the module, outside any routines. It will now be visible to all the module's procedures and functions[VBA]Option Explicit

    Dim doc As Document

    Private Sub CommandButton1_Click()
    Set doc = Documents.Add
    End Sub

    Private Sub CommandButton2_Click()
    If doc Is Nothing Then
    MsgBox "Variable 'doc' not set" & vbLf & _
    "Please click button 1 first"
    Else
    MsgBox "Variable 'doc' set to document: " & doc.Name
    End If
    End Sub[/VBA]An important point to realise is that in a standard module, such a variable will be available all the time the project is loaded. In a userform module, the variable is disposed of with the userform when it is unloaded.
    This illustrates a difference in the "lifetime" of a variable.

    An further level of scope is a "global" variable. If you wanted to unload the userform but still have "doc" available for use, you could declare it in a standard module as Public. It would then be visible to all functions and procedures in the project[VBA]Public doc As Document[/VBA]

    A consequence of increasing the scope of a variable (esp. to global) is that you need to manage when you change as part of your program design, since it's increased visibility means it can easily be changed by a re-used procudure or function at any point.

    You'll find more info and examples of managing scope and lifetime in VBA help and numerous articles on the 'net. It's a crucial concept in programming, so well worth taking the time to research and understand.

    Hope that helps...
    K :-)

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Very nice K. Scope IS a crucial concept, and Saban, it is therefore crucial to understand it, and use it properly. Help is very good, but I also suggest doing actual tests for yourself.

    Make variables that are in scope within a procedure, within a module, and globally. Make tests so you can see where a variable goes out of scope...which is the thing that is so crucial!

    And NEVER forget to destroy object variables that are not being used. Even if you going to create another one just like it. If THAT variable is not being used anymore, destroy it.

    So your set doc = documents.add should always, somewhere, have a set doc = nothing. This is, in fact, a good part of testing for scope.

  4. #4
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Thnx guys

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    Quote Originally Posted by fumei
    Very nice K.
    Agreed
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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