Consulting

Results 1 to 7 of 7

Thread: Use variable in other procedures

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

    Use variable in other procedures

    [VBA]sFileRtfToSave = "R:\Trans\" + sFileRtf
    ActiveDocument.SaveAs sFileRtfToSave, FileFormat:=wdFormatRTF[/VBA]

    This saves the file and puts its name into variable but when procedure ends and macro shifts to another procedure this variable is empty how can I use variable "sFileRtfToSave" in another procedure

    Thnx

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Actually you are incorrect. When your code runs another procedure the variable is NOT empty. It is out of scope, and does not even exist. Variables declared within a procedure only exist within that procedure. When the procedure terminates, all memory addresses assigned to procedure level variables are released.

    Declare it as a public variable. You need to look up Scope in Help. Scope is a very important subject. Do not fall for going too much the other way - making all variables Public. Use variables with the proper scope for what they need to do.

  3. #3
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Scope. Like this.
    K :-)

  4. #4
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Aha or I declare variable public, or rewrite it for each procedure right?
    Instead of making it public I can write it in each procedure? Will this give me the same effect as declaring it public

    thnx

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Not necessarily.
    MyDoc = ActiveDocument.Name can set the value of a Public Variable. MyDoc, used in other routines, will refer to that original value. If that document is closed by the code, how would you delare it in another routine, without hard coding it?
    Regards
    MD
    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'

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Instead of making it public I can write it in each procedure? Will this give me the same effect as declaring it public
    Well, yes of course you can write it each procedure. But No, No, No - it will not have the same effect as declaring it as Public. The value would never be passed from procedure to procedure.

    As stated, a variable declared in a procedure only exists in that procedure. You can have 10 procedures with each one having a variable named myVar. Each myVar is absolutely independent of the others. They ONLY hold value within that procedure.

    Take a look at this.[vba]Sub One()
    Dim sVar As String
    sVar = "Hello"
    Call Two
    MsgBox sVar
    End Sub

    Sub Two()
    Dim sVar As String
    sVar = "Goodbye"
    End Sub[/vba]Two variables, both named sVar. What do you think the message will display? Let's run through.

    In Sub One, sVar becomes "Hello", then control is passed to Sub Two. Yes, THAT sVar becomes "Goodbye". When Sub Two is terminated...so is the sVar in the procedure. Control is passed back to Sub One...and sVar is "Hello". It can NOT be changed by anything that happens in Sub Two, because Sub Two has its own scope.
    [vba]Public sVar As String
    Sub One()
    sVar = "Hello"
    Msgbox sVar ' displays "Hello"
    Call Two
    MsgBox sVar ' displays "Goodbye"
    End Sub

    Sub Two()
    sVar = "Goodbye"
    End Sub[/vba]Message box will display "Hello" then display "Goodbye" The scope of the variable is global, it is set for "Hello" in Sub One, then set for "Goodbye" in Sub Two.

    You MUST look at Scope!

  7. #7
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    thnx man I really apreciate this
    stay cool

Posting Permissions

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