10 Replies Latest reply on May 25, 2011 11:17 PM by OxleyCreek

    Global Variables in Processbook VBA


      I have a display module with two subs and a global variable - startIndex. I set startIndex in the first sub and access it the second sub but the value is not retained when I get to the second sub. What am I doing wrong? Any advice on using global variables in Processbook VBA would be greatly appreciated !




      Michael Jones

      Public startIndex As Integer
      Private Sub CommandButton1_Click()
      Dim val As Value
      Dim theTime As Date
      theTime = Date + #4:00:00 AM#
      If Now < theTime Then theTime = theTime - 1
      Dim maxWidth As Integer
      Dim smallGap As Integer
      startIndex = Symbols.Count + 1
      Dim bigGap As Integer
      Private Sub CommandButton2_Click()
      Dim startTime As Date
      startTime = Date + #4:00:00 AM#
      If Now < startTime Then startTime = startTime + 1
          Dim sym As Symbol
          Dim index As Integer
          index = (Now - startTime) * 48 + startIndex
          Set sym = Symbols.Item(index)
          sym.BackgroundColor = vbGreen
      End Sub


        • Re: Global Variables in Processbook VBA

          Hi Michael,


          Where are you finishing your Sub CommandButton1_Click?


          I am asking, so that you make sure that the body of your CommandButton2_Click is within the body of your Sub CommandButton1_Click.


          As for general tip when using global variables in VBA, you may want to name them with prefix "gbl" or "global", so that you don't forget that they are global. This could save you from a few headaches later on while developing your code.


          Also, you may want to declare your variables all together in the beginning of your Subs, Functions, etc (depending on their scope, of course) and not declare them mixed together with command lines. It helps make the code more readable and maintainable later.