在以下代码中,我要访问中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
。
这有点基本。
当然,您不能ExcelWorkbook1
从另一个子目录访问,因为它是在OpenExcel
子目录中本地声明的。
要ExceWorkBook1
使用两个子访问,必须在这两个子之外声明它。
Dim ExcelWorkbook1 As Workbook 'put this here
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) 'The declaration here is moved outside of the sub, granting access to the other subs
Else
MessageBox.Show("Excel not found!")
End If
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句