Private Sub CmdJoinExcel_Click() '
Dim xlsApp As Object
Dim xlsBook As Object
Dim xlsSheet As Object
Dim xlsApp As New Excel.Application
On Error Resume Next ' 改变错误陷阱
Set xlsApp = GetObject(App.Path, "et.Application")
If Err Then
Err.Clear '清除 Err 对象字段
Set xlsApp = CreateObject("et.Application")
If Err Then
Exit Sub ' 退出程序,以避免进入错误处理程序
End If
End If
xlsApp.Visible = True
Set xlsBook = xlsApp.Workbooks.Add LX = (ComLX.Text)
Select Case LX
Case Is = 1
Set xlsSheet = xlsBook.Worksheets(1) '设定工作表1
Cells(1, 1) = "基本参数": Cells(2, 1) = "名称": Cells(2, 2) = "开式深沟球优化参数": Cells(3, 1) = "系列"
Case Is = 2
Set xlsSheet = xlsBook.Worksheets(2) '设定工作表2
Cells(1, 1) = "基本参数": Cells(2, 1) = "名称": Cells(2, 2) = "密封深沟球优化参数": Cells(3, 1) = "系列"
Case Is = 3
Set xlsSheet = xlsBook.Worksheets(3) '设定工作表3
Cells(1, 1) = "基本参数": Cells(2, 1) = "名称": Cells(2, 2) = "带防尘盖深沟球优化参数": Cells(3, 1) = "系列"
End Select
xlsApp.Save '保存工作簿
xlsBook.Close '关闭工作薄文件
xlsApp.Quit '结束excel对象
Set xlsApp = Nothing '释放xlapp对象得内存空间
Set xlsBook = Nothing
Set xlsSheet = Nothing
End Sub 大神,现在的问题是第一次可以给excel赋值,第二次就变成空的了,不能赋值,求解答,十分感谢啊!
Dim xlsApp As Object
Dim xlsBook As Object
Dim xlsSheet As Object
Dim xlsApp As New Excel.Application
On Error Resume Next ' 改变错误陷阱
Set xlsApp = GetObject(App.Path, "et.Application")
If Err Then
Err.Clear '清除 Err 对象字段
Set xlsApp = CreateObject("et.Application")
If Err Then
Exit Sub ' 退出程序,以避免进入错误处理程序
End If
End If
xlsApp.Visible = True
Set xlsBook = xlsApp.Workbooks.Add LX = (ComLX.Text)
Select Case LX
Case Is = 1
Set xlsSheet = xlsBook.Worksheets(1) '设定工作表1
Cells(1, 1) = "基本参数": Cells(2, 1) = "名称": Cells(2, 2) = "开式深沟球优化参数": Cells(3, 1) = "系列"
Case Is = 2
Set xlsSheet = xlsBook.Worksheets(2) '设定工作表2
Cells(1, 1) = "基本参数": Cells(2, 1) = "名称": Cells(2, 2) = "密封深沟球优化参数": Cells(3, 1) = "系列"
Case Is = 3
Set xlsSheet = xlsBook.Worksheets(3) '设定工作表3
Cells(1, 1) = "基本参数": Cells(2, 1) = "名称": Cells(2, 2) = "带防尘盖深沟球优化参数": Cells(3, 1) = "系列"
End Select
xlsApp.Save '保存工作簿
xlsBook.Close '关闭工作薄文件
xlsApp.Quit '结束excel对象
Set xlsApp = Nothing '释放xlapp对象得内存空间
Set xlsBook = Nothing
Set xlsSheet = Nothing
End Sub 大神,现在的问题是第一次可以给excel赋值,第二次就变成空的了,不能赋值,求解答,十分感谢啊!
解决方案 »
- word2003中如何用vb语言得到一个table的序号?
- MSHFlexGrid列固定问题
- 出现错误"对象变量未设置或With块变量未设置"
- dtpicker控件问题
- vb6 与ACCESS 2003 的对接的问题。
- 利用vb 6.0设计程序实现photoshop的部分功能
- 文本文件压缩原理!请问是如何压缩的?
- 如何远程访问ActiveX文档?急呀!!!!
- Visual studio.NET能运行在Windows 95上吗?
- 关于ObjectContext对象和ObjectControl对象的使用问题,高手们,请你们帮忙解决这个难题。
- VC++6.0工具使用问题
- VBA中运行这段 Do While Range("A1:A65536") <> "" 时 提示运行错误'13' 类型不匹配
Dim xlsApp As Object
Dim xlsBook As Object
Dim xlsSheet As Object
Dim xlscells As Object
On Error Resume Next ' 改变错误陷阱
Set xlsApp = GetObject(App.Path, "excel.Application")
If Err Then
Err.Clear '清除 Err 对象字段
Set xlsApp = CreateObject("excel.Application")
If Err Then
Exit Sub ' 退出程序,以避免进入错误处理程序
End If
End If
xlsApp.Visible = True
Set xlsBook = xlsApp.Workbooks.Add
LX = Val(Text1.Text)
Set xlsSheet = xlsBook.Worksheets(1) '设定工作表1
cells(1, 1) = LX
xlsApp.Save '保存工作簿
xlsBook.Close '关闭工作薄文件
xlsApp.Quit '结束excel对象
Set xlsApp = Nothing '释放xlapp对象得内存空间
Set xlsBook = Nothing
Set xlsSheet = Nothing
End Sub 大神,现在的代码你看看吧,麻烦了啊!
xlsSheet.cells(1, 1) = LX'对象关闭/释放必须按从小到大的次序来!'
Set xlsSheet = Nothing
xlsApp.Save '保存工作簿
xlsBook.Close '关闭工作薄文件
Set xlsBook = Nothing
xlsApp.Quit '结束excel对象
Set xlsApp = Nothing '释放xlapp对象得内存空间
Option Explicit
Sub Test()
On Error Resume Next
Dim xlsApp As Excel.Application, xlsBook As Excel.Workbook
Dim bCreated As Boolean
''首先获取已经打开的Excel对象
Set xlsApp = GetObject(, "Excel.Application")
If xlsApp Is Nothing Then
''如果Excel没有打开,创建一个新的Excel对象
Set xlsApp = CreateObject("Excel.Application")
xlsApp.Visible = True
bCreated = True ''标记这个excel对象是我创建的
End If
''设置工作簿对象
Set xlsBook = xlsApp.Workbooks.Add
''检查工作表的数量,保证至少有3张表
With xlsBook.Worksheets
If .Count < 3 Then
Do While .Count < 3
.Add ''循环添加新工作表
Loop
End If
End With
''开始填充数据
With xlsBook.Worksheets(1) ''第1张表
.Cells(1, 1) = "基本参数"
.Cells(2, 1) = "名称"
.Cells(2, 2) = "开式深沟球优化参数"
.Cells(3, 1) = "系列"
End With
With xlsBook.Worksheets(2) ''第2张表
.Cells(1, 1) = "基本参数"
.Cells(2, 1) = "名称"
.Cells(2, 2) = "密封深沟球优化参数"
.Cells(3, 1) = "系列"
End With
With xlsBook.Worksheets(3) ''第3张表
.Cells(1, 1) = "基本参数"
.Cells(2, 1) = "名称"
.Cells(2, 2) = "带防尘盖深沟球优化参数"
.Cells(3, 1) = "系列"
End With
xlsBook.Save ''保存工作簿
xlsBook.Close ''关闭工作簿
Set xlsBook = Nothing ''卸载工作簿对象
If bCreated Then xlsApp.Quit ''如果是自己创建的excel对象则关闭
Set xlsApp = Nothing ''卸载Excel对象
End Sub..