title: How to access variables declared in one sub in another sub in vb.net
body: <p>In the following code, I want to access <code>ExcelWorkbook1</code> declared in <code>Sub OpenExcel</code> in <code>Sub SelectRangeOfCells</code>. Please help me with the same.</p>
  
  <pre><code>Public Sub OpenExcel(ByVal Path As String, ByVal Filename As String)
      Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
  
      xlApp.Visible = True
      If xlApp Is Nothing Then
          MessageBox.Show("Excel is not properly installed!!")
  
      End If
      Dim MyFile As String = Dir$(Path + "\" + Filename)
      If MyFile = Filename Then
          Dim ExcelWorkbook1 As Workbook = xlApp.Workbooks.Open(Path + "\" + Filename)
      Else
          MessageBox.Show("Excel not found!")
      End If
  End Sub
  
  Public Sub SelectRangeOfCells(ByVal Sheet As String, ByVal Range As String)
      ExcelWorkbook1.Worksheets(Sheet).activate
  End Sub
  </code></pre>
  
  <p>This shows error in <code>SelectRangeOfCells</code> Sub that <code>ExcelWorkbook1</code> is not declared.</p>