Results 1 to 15 of 15

Thread: Does VBA Lose Variable Definitions and/or Values between Subs?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,954
    Location
    Variables are private unless you make the Public outside of a Sub or Dim outside of Sub.

    Undefined variables are sometimes mistaken due to spelling errors. Other times, if not set as another part expects, it will show a problem.

    e.g.
    [VBA]
    Public s as String
    Sub t
    s="Hello World!"
    tt
    End Sub

    Sub tt
    MsgBox s
    End Sub
    [/VBA]

  2. #2
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Quote Originally Posted by Kenneth Hobs
    Variables are private unless you make the Public outside of a Sub or Dim outside of Sub.

    Undefined variables are sometimes mistaken due to spelling errors. Other times, if not set as another part expects, it will show a problem.
    DOH! I've either lost too many brain cells or had a brain fart. Thanks, Kenneth for pointing that out. Instead of DIMing, I need to be PUBLICing the variables that are shared.

    This makes sense. Is a public variable visible inside a Function or just in Subs?

    Thanks,
    Ron
    Windermere, FL

Posting Permissions

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