在以下代码中,我想访问ExcelWorkbook1
中Sub OpenExcel
中声明的Sub SelectRangeOfCells
。请帮助我。
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
这显示SelectRangeOfCells
Sub中未声明ExcelWorkbook1
的错误。
答案 0 :(得分:1)
这有点基础。
当然,您无法从其他子访问/repo-name/bower-elements/some-element/some-element.html
,因为它是在ExcelWorkbook1
子区域中声明的。
要使用两个潜艇访问OpenExcel
,您必须在这两个潜艇之外声明它。
ExceWorkBook1
答案 1 :(得分:1)
您必须将ExcelWorkbook1
声明为类属性,然后在您的方法中使用它:
Property ExcelWorkbook1 As Workbook
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
ExcelWorkbook1 = 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
有关VB.NET中的属性的更多信息,您可以从MSDN
学习