如何打开一个Excel文件,同时调用多个Sheet??
我现在编了一个程序,实现了打开一个文件,直接调用Sheet表格数据,主程序摘要如下:
strFile = ThisDrawing.Application.VBE.ActiveVBProject.FileName
' 运行Excel应用程序
Set excelApp = CreateObject( "Excel.Application ")
excelApp.Visible = True
' 打开指定的Excel文件,获得指定的页
excelApp.Workbooks.Open Left$(strFile, Len(strFile) - 15) & "demo.xls "
Set excelSheet = excelApp.ActiveWorkbook.Sheets(SheetName) '只能打开一个表.
我的需求是:
在调用程序中运行
strFile = ThisDrawing.Application.VBE.ActiveVBProject.FileName
' 运行Excel应用程序
Set excelApp = CreateObject( "Excel.Application ")
excelApp.Visible = True
' 打开指定的Excel文件,获得指定的页
excelApp.Workbooks.Open Left$(strFile, Len(strFile) - 15) & "demo.xls "
在功能模块中执行
Set excelSheet = excelApp.ActiveWorkbook.Sheets(SheetName)
我试了很多办法,没有通过.原因是excelApp.ActiveWorkbook与excelApp.Visible之间不能产生数据关联!
详细程序如下
Sub Tube()
'
Dim RevolveSolidEntity As AcadEntity
Set RevolveSolidEntity = RevolveSolid_Excel( "R02_5 ", 11)
Set RevolveSolidEntity = RevolveSolid_Excel( "R02_6 ", 9)
End Sub
Private Function RevolveSolid_Excel(ByVal SheetName As String, ByVal ShellRow As Integer) As AcadEntity
Dim excelApp As Excel.Application
Dim excelSheet As Excel.Worksheet
Dim curves() As AcadEntity
ReDim curves(0 To ShellRow - 1) As AcadEntity
' 获得当前工程的路径
Dim strFile As String
strFile = ThisDrawing.Application.VBE.ActiveVBProject.FileName
' 运行Excel应用程序
Set excelApp = CreateObject( "Excel.Application ")
excelApp.Visible = True
' 打开指定的Excel文件,获得指定的页
excelApp.Workbooks.Open Left$(strFile, Len(strFile) - 15) & "demo.xls "
Set excelSheet = excelApp.ActiveWorkbook.Sheets(SheetName)
' 使用指定页的数据绘图
Dim lineObj As AcadLine
Dim startPoint(0 To 2) As Double
Dim endPoint(0 To 2) As Double
Dim ii As Integer
For ii = 1 To ShellRow
startPoint(0) = excelSheet.Cells(ii, 1).Value
Debug.Print startPoint(0)
startPoint(1) = excelSheet.Cells(ii, 2).Value
startPoint(2) = excelSheet.Cells(ii, 3).Value
endPoint(0) = excelSheet.Cells(ii + 1, 1).Value
endPoint(1) = excelSheet.Cells(ii + 1, 2).Value
endPoint(2) = excelSheet.Cells(ii + 1, 3).Value
Set curves(ii - 1) = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)
curves(ii - 1).color = 4
Next ii
' 退出Excel应用程序
excelApp.Quit
End Function
[解决办法]
帮忙顶一下!
不知道你的问题解决了没?我也遇到同样的问题.