VB导出完EXCEL并另存为,保存后不能释放EXCEL,急急!!!!!!

abc_2004 2008-06-28 03:29:59
语言VB6.0,引用EXCEL 10
我想要导出完EXCEL报表并另存为,保存后释放EXCEL
>>>>>现在问题是另存为保存都可以,但不能释放EXCEL,请各位大侠帮忙,谢谢!.....
On Error Resume Next
If MSFlexGrid1.TextMatrix(1, 2) = "" Then
MsgBox "没有数据导出", vbInformation, "提示"
Exit Sub
End If
Dim excelApp As Excel.Application
Set excelApp = New Excel.Application
If excelApp Is Nothing Then
Set excelApp = CreateObject("Excel.application")
If excelApp Is Nothing Then
Exit Sub
End If
End If
excelApp.Visible = True
Me.MousePointer = vbHourglass
excelApp.Workbooks.Add
With excelApp.ActiveSheet
ActiveSheet.Range(Cells(1, 1), Cells(4, 3)).Merge '合并
excelApp.ActiveSheet.rows(1).RowHeight = 15 '第一行行高
ActiveSheet.Range(Cells(1, 4), Cells(2, 4)).Merge '合并
excelApp.ActiveSheet.rows(2).RowHeight = 15 '第二行行高
ActiveSheet.Range(Cells(3, 4), Cells(4, 4)).Merge '合并
excelApp.ActiveSheet.rows(3).RowHeight = 15 '第三行行高
excelApp.ActiveSheet.rows(4).RowHeight = 15 '第四行行高
ActiveSheet.Range(Cells(1, 5), Cells(2, 15)).Merge '合并
ActiveSheet.Range(Cells(3, 5), Cells(4, 15)).Merge '合并
.Cells(1, 4) = "制度名称"
ActiveSheet.Cells(1, 4).HorizontalAlignment = xlCenter '居中
.Cells(3, 4) = "表格名称"
ActiveSheet.Cells(3, 4).HorizontalAlignment = xlCenter '居中
.Cells(1, 5) = "生产成本统计"
ActiveSheet.Cells(1, 5).HorizontalAlignment = xlCenter '居中
.Cells(3, 5) = "产成品生产与成本统计月累积报表"
ActiveSheet.Cells(3, 5).HorizontalAlignment = xlCenter '居中
.Cells(1, 16) = "填表:"
ActiveSheet.Cells(1, 16).HorizontalAlignment = xlCenter '居中
.Cells(2, 16) = "审核:"
ActiveSheet.Cells(2, 16).HorizontalAlignment = xlCenter '居中
.Cells(3, 16) = "批准:"
ActiveSheet.Cells(3, 16).HorizontalAlignment = xlCenter '居中
.Cells(4, 16) = "归档部门:"
ActiveSheet.Cells(4, 16).HorizontalAlignment = xlCenter '居中
.Cells(1, 18) = "制度编号:"
ActiveSheet.Cells(1, 18).HorizontalAlignment = xlCenter '居中
.Cells(2, 18) = "制度版本:"
ActiveSheet.Cells(2, 18).HorizontalAlignment = xlCenter '居中
.Cells(3, 18) = "表格编号:"
ActiveSheet.Cells(3, 18).HorizontalAlignment = xlCenter '居中
.Cells(4, 18) = "表格版本:"
ActiveSheet.Cells(4, 18).HorizontalAlignment = xlCenter '居中
Worksheets("Sheet1").Range("A1:s4").Borders.LineStyle = xlContinuous '设置横线(边框)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
.Cells(5, 17) = "报表日期:" & Format$(Date)
ActiveSheet.Cells(5, 17).Font.Size = 10 '字体大小
''''''''''''''''表头内容''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Worksheets("Sheet1").Range("A6:s6").Borders.LineStyle = xlContinuous '设置横线(边框)
excelApp.ActiveSheet.rows(6).RowHeight = 18 '第六行行高
excelApp.ActiveSheet.Cells(1).ColumnWidth = 5 '第一列
.Cells(6, 1).Value = "序号"
excelApp.ActiveSheet.Cells(2).ColumnWidth = 0 '第一列
.Cells(6, 2).Value = ""
excelApp.ActiveSheet.Cells(3).ColumnWidth = 16 '第二列
.Cells(6, 3).Value = "产品型号"
excelApp.ActiveSheet.Cells(4).ColumnWidth = 12 '第三列
.Cells(6, 4).Value = "产品批号"
excelApp.ActiveSheet.Cells(5).ColumnWidth = 12 '第四列
.Cells(6, 5).Value = "产品月批次"
excelApp.ActiveSheet.Cells(6).ColumnWidth = 8 '第五列
.Cells(6, 6).Value = "实际产量"
excelApp.ActiveSheet.Cells(7).ColumnWidth = 8 '第六列
.Cells(6, 7).Value = "实际金额"
excelApp.ActiveSheet.Cells(8).ColumnWidth = 12 '第七列
.Cells(6, 8).Value = "实际进仓数量"
excelApp.ActiveSheet.Cells(9).ColumnWidth = 12 '第八列
.Cells(6, 9).Value = "实际进仓金额"
excelApp.ActiveSheet.Cells(10).ColumnWidth = 12 '第九列
.Cells(6, 10).Value = "车间存放量"
excelApp.ActiveSheet.Cells(11).ColumnWidth = 12 '第七列
.Cells(6, 11).Value = "车间存放金额"
excelApp.ActiveSheet.Cells(12).ColumnWidth = 12 '第八列
.Cells(6, 12).Value = "材料成本单价"
excelApp.ActiveSheet.Cells(13).ColumnWidth = 12 '第九列
.Cells(6, 13).Value = "包装成本单价"
excelApp.ActiveSheet.Cells(14).ColumnWidth = 12 '第十列
.Cells(6, 14).Value = "材包成本单价"
excelApp.ActiveSheet.Cells(15).ColumnWidth = 12 '第十一列
.Cells(6, 15).Value = "材包税成本"
excelApp.ActiveSheet.Cells(16).ColumnWidth = 12 '第十二列
.Cells(6, 16).Value = "损耗量"
excelApp.ActiveSheet.Cells(17).ColumnWidth = 10 '第一列
.Cells(6, 17).Value = "损耗率"
excelApp.ActiveSheet.Cells(18).ColumnWidth = 12 '第十三列
.Cells(6, 18).Value = "产出率"
excelApp.ActiveSheet.Cells(19).ColumnWidth = 12 '第十四列
.Cells(6, 19).Value = "备注"
''''''''''''''''''''''''''''''标题名称'''''''''''''''''''''''''''''''''
.Range("q7:q50").NumberFormat = "0.0%"
.Range("r7:r50").NumberFormat = "0.0%"
Dim i As Long
Dim j As Integer
For i = 1 To MSFlexGrid1.rows
For j = 0 To MSFlexGrid1.Cols
Cells(i + 6, j + 1).Value = "" & Format$(MSFlexGrid1.TextMatrix(i, j))
Next j
.Cells(MSFlexGrid1.rows + 6, 1) = "本月合计:"
ActiveSheet.Range(Cells(MSFlexGrid1.rows + 6, 1), Cells(MSFlexGrid1.rows + 6, 3)).Merge '合并
.Cells(MSFlexGrid1.rows + 7, 1) = "上月累计:"
ActiveSheet.Range(Cells(MSFlexGrid1.rows + 7, 1), Cells(MSFlexGrid1.rows + 7, 3)).Merge '合并
.Cells(MSFlexGrid1.rows + 8, 1) = "本年累计:"
ActiveSheet.Range(Cells(MSFlexGrid1.rows + 8, 1), Cells(MSFlexGrid1.rows + 8, 3)).Merge '合并
Worksheets("Sheet1").Range(Cells(7, 1), Cells(MSFlexGrid1.rows + 8, 19)).Borders.LineStyle = xlContinuous '设置横线(边框)
Cells(X, Y).Value
Next i
'''''''''''''''''''''''''''''''''显示MSFlexGrid内容'''''''''''''''''''''''
.Range("A7:y50").WrapText = True '设置单元格内文字是否自动换行
.Range("A7:u50").RowHeight = 18 '设置单元格高度
.Range("A6:y50").HorizontalAlignment = 3 '设置字体居中显示
End With
Dim abc As String
abc = Format$(Date, "yyyymmdd") & "产成品生产与成本统计月累积报表"
aa = Application.Dialogs(xlDialogSaveAs).Show(abc)
excelApp.Workbooks(1).Saved = True
Application.ScreenUpdate = False
Me.MousePointer = 0

aa.Close'关闭另存为
ActiveSheet.Close'关闭
excelApp.Quit'关闭
Set excelApp = Nothing'释放excel
MsgBox "导出成功!", vbOKOnly + vbInformation, "消息提示"
Application.ScreenUpdate = True
...全文
411 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
abc_2004 2008-06-29
  • 打赏
  • 举报
回复
经自已努力问题解决了,谢谢各位大侠关注......
Dim abc As String
Dim i As Long
Dim j As Integer
On Error Resume Next
If MSFlexGrid1.TextMatrix(1, 2) = "" Then
MsgBox "没有数据导出", vbInformation, "提示"
Exit Sub
End If
Dim excelApp As Excel.Application
Set excelApp = New Excel.Application
Set excelApp = CreateObject("Excel.Application")
Dim exbook As Excel.Workbook
Dim exsheet As Excel.Worksheet
Set exbook = excelApp.Workbooks.Add
excelApp.SheetsInNewWorkbook = 1
excelApp.Visible = False
excelApp.UserControl = True
Me.MousePointer = vbHourglass
With excelApp.ActiveSheet '表头合并
.Range("a1:b4").Merge '合并
.Range("c1:d2").Merge '合并
.Range("c3:d4").Merge '合并
.Range("e1:o2").Merge '合并
.Range("e3:o4").Merge '合并
.Range("c1:d2") = "制度名称"
.Range("c3:d4") = "表格名称"
.Range("e1:o2") = "生产成本统计"
.Range("e3:o4") = "产成品生产与成本统计月累积报表"
.Range("p1:p1") = "填表:"
.Range("p2:p2") = "审核:"
.Range("p3:p3") = "批准:"
.Range("p4:p4") = "归档部门:"
.Range("r1:r1") = "制度编号:"
.Range("r2:r2") = "制度版本:"
.Range("r3:r3") = "表格编号:"
.Range("r4:r4") = "表格版本:"
.rows.HorizontalAlignment = xlVAlignCenter
End With
With excelApp.ActiveSheet
.Range("A1:s4").Borders.LineStyle = xlContinuous '表头边框线
End With
With excelApp.ActiveSheet
.Range("q5:q5") = "报表日期:" & Format$(Date, "yyyy-mm-dd")
End With
With excelApp.ActiveSheet
.Range("a6:a6") = "序号"
.Range("b6:b6") = ""
.Range("c6:c6") = "产品型号"
.Range("d6:d6") = "产品批号"
.Range("e6:e6") = "产品月批次"
.Range("f6:f6") = "实际产量"
.Range("g6:g6") = "实际金额"
.Range("h6:h6") = "实际进仓数量"
.Range("i6:i6") = "实际进仓金额"
.Range("j6:j6") = "车间存放量"
.Range("k6:k6") = "车间存放金额"
.Range("l6:l6") = "材料成本单价"
.Range("m6:m6") = "包装成本单价"
.Range("n6:n6") = "材包成本单价"
.Range("o6:o6") = "材包税成本"
.Range("p6:p6") = "损耗量"
.Range("q6:q6") = "损耗率"
.Range("r6:r6") = "产出率"
.Range("s6:s6") = "备注"
End With
With excelApp.ActiveSheet
.Cells(1).ColumnWidth = 9
.Cells(2).ColumnWidth = 0 '第一列
.Cells(3).ColumnWidth = 16 '第二列
.Cells(4).ColumnWidth = 12 '第三列
.Cells(5).ColumnWidth = 12 '第四列
.Cells(6).ColumnWidth = 8 '第五列
.Cells(7).ColumnWidth = 8 '第六列
.Cells(8).ColumnWidth = 12 '第七列
.Cells(9).ColumnWidth = 12 '第八列
.Cells(10).ColumnWidth = 12 '第九列
.Cells(11).ColumnWidth = 12 '第七列
.Cells(12).ColumnWidth = 12 '第八列
.Cells(13).ColumnWidth = 12 '第九列
.Cells(14).ColumnWidth = 12 '第十列
.Cells(15).ColumnWidth = 12 '第十一列
.Cells(16).ColumnWidth = 12 '第十二列
.Cells(17).ColumnWidth = 10 '第一列
.Cells(18).ColumnWidth = 12 '第十三列
.Cells(19).ColumnWidth = 12 '第十四列
End With
With excelApp.ActiveSheet
.Range("A6:s6").Borders.LineStyle = xlContinuous '表头边框线
End With
'''''''''''''''''''''''''''标题名称'''''''''''''''''''''''''''''''''
' .Range("q7:q50").NumberFormat = "0.0%"
' .Range("r7:r50").NumberFormat = "0.0%"
With excelApp.ActiveSheet
For i = 1 To MSFlexGrid1.rows
For j = 0 To MSFlexGrid1.Cols
.Cells(i + 6, j + 1).Value = "" & Format$(MSFlexGrid1.TextMatrix(i, j))
Next j
.Range("a" & MSFlexGrid1.rows + 6 & ":" & "a" & MSFlexGrid1.rows + 6) = "本月合计:"
.Range("a" & MSFlexGrid1.rows + 6 & ":" & "b" & MSFlexGrid1.rows + 6).Merge '合并
.Cells(MSFlexGrid1.rows + 6, 6).Value = "" & MSFlexGrid2.TextMatrix(0, 5)
.Cells(MSFlexGrid1.rows + 6, 7).Value = "" & MSFlexGrid2.TextMatrix(0, 6)
.Cells(MSFlexGrid1.rows + 6, 8).Value = "" & MSFlexGrid2.TextMatrix(0, 7)
.Cells(MSFlexGrid1.rows + 6, 9).Value = "" & MSFlexGrid2.TextMatrix(0, 8)
.Cells(MSFlexGrid1.rows + 6, 10).Value = "" & MSFlexGrid2.TextMatrix(0, 9)
.Cells(MSFlexGrid1.rows + 6, 11).Value = "" & MSFlexGrid2.TextMatrix(0, 10)
.Cells(MSFlexGrid1.rows + 6, 12).Value = "" & MSFlexGrid2.TextMatrix(0, 11)
.Cells(MSFlexGrid1.rows + 6, 13).Value = "" & MSFlexGrid2.TextMatrix(0, 12)
.Cells(MSFlexGrid1.rows + 6, 14).Value = "" & MSFlexGrid2.TextMatrix(0, 13)
.Cells(MSFlexGrid1.rows + 6, 15).Value = "" & MSFlexGrid2.TextMatrix(0, 14)
.Cells(MSFlexGrid1.rows + 6, 16).Value = "" & MSFlexGrid2.TextMatrix(0, 15)
.Cells(MSFlexGrid1.rows + 6, 17).Value = "" & MSFlexGrid2.TextMatrix(0, 16)
.Cells(MSFlexGrid1.rows + 6, 18).Value = "" & MSFlexGrid2.TextMatrix(0, 17)
.Range("a" & MSFlexGrid1.rows + 7 & ":" & "a" & MSFlexGrid1.rows + 7) = "上月累计:"
.Range("a" & MSFlexGrid1.rows + 7 & ":" & "b" & MSFlexGrid1.rows + 7).Merge '合并
.Cells(MSFlexGrid1.rows + 7, 6).Value = "" & MSFlexGrid2.TextMatrix(1, 5)
.Cells(MSFlexGrid1.rows + 7, 7).Value = "" & MSFlexGrid2.TextMatrix(1, 6)
.Cells(MSFlexGrid1.rows + 7, 8).Value = "" & MSFlexGrid2.TextMatrix(1, 7)
.Cells(MSFlexGrid1.rows + 7, 9).Value = "" & MSFlexGrid2.TextMatrix(1, 8)
.Cells(MSFlexGrid1.rows + 7, 10).Value = "" & MSFlexGrid2.TextMatrix(1, 9)
.Cells(MSFlexGrid1.rows + 7, 11).Value = "" & MSFlexGrid2.TextMatrix(1, 10)
.Cells(MSFlexGrid1.rows + 7, 12).Value = "" & MSFlexGrid2.TextMatrix(1, 11)
.Cells(MSFlexGrid1.rows + 7, 13).Value = "" & MSFlexGrid2.TextMatrix(1, 12)
.Cells(MSFlexGrid1.rows + 7, 14).Value = "" & MSFlexGrid2.TextMatrix(1, 13)
.Cells(MSFlexGrid1.rows + 7, 15).Value = "" & MSFlexGrid2.TextMatrix(1, 14)
.Cells(MSFlexGrid1.rows + 7, 16).Value = "" & MSFlexGrid2.TextMatrix(1, 15)
.Cells(MSFlexGrid1.rows + 7, 17).Value = "" & MSFlexGrid2.TextMatrix(1, 16)
.Cells(MSFlexGrid1.rows + 7, 18).Value = "" & MSFlexGrid2.TextMatrix(1, 17)

.Range("a" & MSFlexGrid1.rows + 8 & ":" & "a" & MSFlexGrid1.rows + 8) = "本年累计:"
.Range("a" & MSFlexGrid1.rows + 8 & ":" & "b" & MSFlexGrid1.rows + 8).Merge '合并
.Cells(MSFlexGrid1.rows + 8, 6).Value = "" & MSFlexGrid2.TextMatrix(2, 5)
.Cells(MSFlexGrid1.rows + 8, 7).Value = "" & MSFlexGrid2.TextMatrix(2, 6)
.Cells(MSFlexGrid1.rows + 8, 8).Value = "" & MSFlexGrid2.TextMatrix(2, 7)
.Cells(MSFlexGrid1.rows + 8, 9).Value = "" & MSFlexGrid2.TextMatrix(2, 8)
.Cells(MSFlexGrid1.rows + 8, 10).Value = "" & MSFlexGrid2.TextMatrix(2, 9)
.Cells(MSFlexGrid1.rows + 8, 11).Value = "" & MSFlexGrid2.TextMatrix(2, 10)
.Cells(MSFlexGrid1.rows + 8, 12).Value = "" & MSFlexGrid2.TextMatrix(2, 11)
.Cells(MSFlexGrid1.rows + 8, 13).Value = "" & MSFlexGrid2.TextMatrix(2, 12)
.Cells(MSFlexGrid1.rows + 8, 14).Value = "" & MSFlexGrid2.TextMatrix(2, 13)
.Cells(MSFlexGrid1.rows + 8, 15).Value = "" & MSFlexGrid2.TextMatrix(2, 14)
.Cells(MSFlexGrid1.rows + 8, 16).Value = "" & MSFlexGrid2.TextMatrix(2, 15)
.Cells(MSFlexGrid1.rows + 8, 17).Value = "" & MSFlexGrid2.TextMatrix(2, 16)
.Cells(MSFlexGrid1.rows + 8, 18).Value = "" & MSFlexGrid2.TextMatrix(2, 17)
.Range("a" & 7 & ":" & "s" & MSFlexGrid1.rows + 8).Borders.LineStyle = xlContinuous '设置横线(边框)
Next i
End With
With excelApp
abc = Format$(Date, "yyyymmdd") & "产成品生产与成本统计月累积报表"
aa = .Dialogs(xlDialogSaveAs).Show(abc)
.Workbooks(1).Saved = True
End With
Me.MousePointer = 0
exbook.Close (True)
excelApp.Quit
Set exsheet = Nothing
Set exbook = Nothing
Set excelApp = Nothing
MsgBox "导出成功!", vbOKOnly + vbInformation, "消息提示"

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧