title: Accessing Global Variables in VBA (Excel)
body: <p>I've never used global variables in VBA, but I understand global variables are instantiated outside of function/sub declarations?</p>
  
  <p>I have a global (public) variable declared at the top of a module which is then given a value of 0 by a subroutine within the same module.</p>
  
  <pre><code>Option Explicit
  Public NumNodes As Integer
  
  Sub Inst_Glob_Vars()
  NumNodes = 0
  End Sub
  </code></pre>
  
  <p>This subroutine is called whenever the workbook is opened (sub is called in the "ThisWorkbook" object) which will also instantiate the global variable and set the 0 value. </p>
  
  <pre><code>Option Explicit
  
  Private Sub Workbook_Open()
  Call Inst_Glob_Vars
  End Sub
  </code></pre>
  
  <p>I have a button in the excel sheet which, when clicked, will increment this global variable. The definition for this button is in the Sheet1 object. </p>
  
  <pre><code>Private Sub CommandButton2_Click()
  'NumNodes = NumNodes + 1
  Debug.Print "NumNodes = " &amp; NumNodes 'Debug
  End Sub
  </code></pre>
  
  <p>Do I need to declare the global/public variables in every module/object the variable is used? Every time I click the button, the variable is not incrementing but giving a Null/Blank value when debugging. I am for sure not declaring my global variable correctly but not sure where I'm making mistakes.   </p>
  
  <p>Update: Here is the updated command button sub. If I comment out second sub call (Node_Button_Duplication), everything works fine. Chances are it might be that sub which is causing problems... </p>
  
  <pre><code>Private Sub CommandButton2_Click()
  Call Channel_Selection_Duplication
  Call Node_Button_Duplication
  NumNodes = NumNodes + 1
  Debug.Print "NumNodes = " &amp; NumNodes 'Debug
  End Sub
  </code></pre>
  
  <p>Both Channel_Selection_Duplication and Node_Button_Duplication are both defined in the same seperate module:</p>
  
  <pre><code>Option Explicit
  
  Public Sub Channel_Selection_Duplication()
  '
  ' Description: Macro which duplicates the 'Channel Usage Selection' columns at a specific cell reference
  
      Range("Q8:S8").Select
      With Selection
          .HorizontalAlignment = xlCenter
          .VerticalAlignment = xlBottom
          .WrapText = False
          .Orientation = 0
          .AddIndent = False
          .IndentLevel = 0
          .ShrinkToFit = False
          .ReadingOrder = xlContext
          .MergeCells = False
      End With
      Selection.Merge
      Range("Q8:S8").Select
      ActiveCell.FormulaR1C1 = "Channel Usage Selection"
      Range("Q8:S52").Select
      Range("Q52").Activate
      Selection.Borders(xlDiagonalDown).LineStyle = xlNone
      Selection.Borders(xlDiagonalUp).LineStyle = xlNone
      With Selection.Borders(xlEdgeLeft)
          .LineStyle = xlContinuous
          .Weight = xlThin
          .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeTop)
          .LineStyle = xlContinuous
          .Weight = xlThin
          .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeBottom)
          .LineStyle = xlContinuous
          .Weight = xlThin
          .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlEdgeRight)
          .LineStyle = xlContinuous
          .Weight = xlThin
          .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlInsideVertical)
          .LineStyle = xlContinuous
          .Weight = xlThin
          .ColorIndex = xlAutomatic
      End With
      With Selection.Borders(xlInsideHorizontal)
          .LineStyle = xlContinuous
          .Weight = xlThin
          .ColorIndex = xlAutomatic
      End With
      Range("Q8:S8").Select
      Selection.Interior.ColorIndex = 36
  
  'NumNodes = NumNodes + 1
  'Debug.Print NumNodes
  End Sub
  
  Public Sub Node_Button_Duplication()
  
      ActiveSheet.Shapes("CommandButton1").Select
      Selection.Copy
      Range("Q5").Select
      ActiveSheet.Paste
      Selection.ShapeRange.IncrementTop -14.25
  End Sub
  </code></pre>