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

    Variables (XL97:SR2)

    If I declare a variable in the "ThisWorkbook" object as a public variable should I not be able to use the variable in a module?

    Example: Within the workbook object-
    Public Var1 as String

    Private Sub Workbook_Open()
    Var1 = "Yes"
    End Sub

    Within a Module-
    Sub Start()
    Dim Var2 as String
    Var2 = "It Works"

    TextLine = ""& Var1 &" "& Var2 &"
    End Sub

    The variable "TextLine" should read "Yes It Works"

    Thanks,
    John

  2. #2
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Variables (XL97:SR2)

    Public Var1 as String

    should be in the declaration section of a module

    Steve

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

    Re: Variables (XL97:SR2)

    Steve,

    I've added the "Public Var1 As String" to the module and the variable "Var1" is empty. It appears the declared variable in the "ThisWorkbook" object is not being passed.

    John

  4. #4
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Variables (XL97:SR2)

    Take the Public Var1 as string out of the ThisWorkbok.

    Check to see if it is being reset anywhare by adding a watch to it.

    Highlight Var1 and right click. Select Add Watch.

    Expression should be Var1
    Change the Procedure Combo to (All Procedures)
    Change the Module Combo to (All Modules)

    Click OK. and then step through the code. You should be able to see where it changes. If you take out the Public line out of ThisWorkbook, then it should not change except for when you assign the value Yes to it.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  5. #5
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Variables (XL97:SR2)

    Did you save and reopen the workbook?
    Var1 gets a value only when you open the workbook. It eorked for me in Excel97

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

    Re: Variables (XL97:SR2)

    Your suggestion worked.

    Thanks

Posting Permissions

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