VB导出完EXCEL并另存为,保存后不能释放EXCEL,急急!!!!!!
语言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