这段写EXCEL报表的程序运行时出现异常!

lcj49997 2005-10-04 09:10:07
一个产生日报的函数如下,在有些机器上调用时会出现异常,大致是:Excel应用程序关闭...在创建错误日志。报表还是正确生成了,就是每次调用的时候弹出一个异常,很不方便的,请高手帮我看一下是不是代码有问题。
Private Sub input_ribao(strdate As String)

Dim strsource1, strsource, strdestination, strdestination1, strdestination2, strdestination3 As String

Dim xlapp As Excel.Application
Dim xlbook1 As Excel.Workbook
Dim xlsheet1 As Excel.Worksheet

Dim xlbook2 As Excel.Workbook
Dim xlsheet2 As Excel.Worksheet
Dim xlbook3 As Excel.Workbook
Dim xlsheet3 As Excel.Worksheet

Set xlapp = New Excel.Application
Set xlapp = CreateObject("Excel.Application")

strsource1 = "d:\template\template1.xls"
strdestination1 = "d:\baobiaoku\banbao_beiyong\" + Format(strdate, "yyyy_mm_dd") + "_00" + ".xls"
If Not fileexists(strdestination1) Then
FileCopy strsource1, strdestination1
End If
Set xlbook1 = xlapp.Workbooks.Open(strdestination1)
Set xlsheet1 = xlbook1.Sheets(1)

strdestination2 = "d:\baobiaoku\banbao_beiyong\" + Format(strdate, "yyyy_mm_dd") + "_08" + ".xls"
If Not fileexists(strdestination2) Then
FileCopy strsource1, strdestination2
End If
Set xlbook2 = xlapp.Workbooks.Open(strdestination2)
Set xlsheet2 = xlbook2.Sheets(1)

strdestination3 = "d:\baobiaoku\banbao_beiyong\" + Format(strdate, "yyyy_mm_dd") + "_16" + ".xls"
If Not fileexists(strdestination3) Then
FileCopy strsource1, strdestination3
End If
Set xlbook3 = xlapp.Workbooks.Open(strdestination3)
Set xlsheet3 = xlbook3.Sheets(1)


strdestination = "d:\baobiaoku\ribao\ribao.xls"
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
If Not fileexists(strdestination) Then
strsource1 = "d:\template\template2.xls"
FileCopy strsource1, strdestination
End If
Set xlbook = xlapp.Workbooks.Open(strdestination)
Set xlsheet = xlbook.Sheets(1)

Dim L As Single
L = 7
xlsheet.Cells(L, 5) = xlsheet1.Cells(25 - L, 4) + xlsheet2.Cells(25 - L, 4) + xlsheet3.Cells(25 - L, 4)
xlsheet.Cells(L, 7) = xlsheet1.Cells(25 - L, 7) + xlsheet2.Cells(25 - L, 7) + xlsheet3.Cells(25 - L, 7)
xlsheet.Cells(L, 9) = xlsheet1.Cells(25 - L, 10) + xlsheet2.Cells(25 - L, 10) + xlsheet3.Cells(25 - L, 10)
xlsheet.Cells(L, 11) = xlsheet1.Cells(25 - L, 13) + xlsheet2.Cells(25 - L, 13) + xlsheet3.Cells(25 - L, 13)
xlsheet.Cells(L, 6) = xlsheet1.Cells(26 - L, 4) + xlsheet2.Cells(26 - L, 4) + xlsheet3.Cells(26 - L, 4)
xlsheet.Cells(L + 1, 6) = xlsheet1.Cells(27 - L, 4) + xlsheet2.Cells(27 - L, 4) + xlsheet3.Cells(27 - L, 4)
xlsheet.Cells(L + 2, 6) = xlsheet1.Cells(28 - L, 4) + xlsheet2.Cells(28 - L, 4) + xlsheet3.Cells(28 - L, 4)
xlsheet.Cells(L + 3, 6) = xlsheet1.Cells(29 - L, 4) + xlsheet2.Cells(29 - L, 4) + xlsheet3.Cells(29 - L, 4)
xlsheet.Cells(L + 4, 6) = xlsheet1.Cells(30 - L, 4) + xlsheet2.Cells(30 - L, 4) + xlsheet3.Cells(30 - L, 4)
xlsheet.Cells(L, 8) = xlsheet1.Cells(26 - L, 7) + xlsheet2.Cells(26 - L, 7) + xlsheet3.Cells(26 - L, 7)
xlsheet.Cells(L + 2, 8) = xlsheet1.Cells(28 - L, 7) + xlsheet2.Cells(28 - L, 7) + xlsheet3.Cells(28 - L, 7)
xlsheet.Cells(L + 1, 8) = xlsheet1.Cells(27 - L, 7) + xlsheet2.Cells(27 - L, 7) + xlsheet3.Cells(27 - L, 7)
xlsheet.Cells(L + 3, 8) = xlsheet1.Cells(29 - L, 7) + xlsheet2.Cells(29 - L, 7) + xlsheet3.Cells(29 - L, 7)
xlsheet.Cells(L + 4, 8) = xlsheet1.Cells(30 - L, 7) + xlsheet2.Cells(30 - L, 7) + xlsheet3.Cells(30 - L, 7)
xlsheet.Cells(L, 10) = xlsheet1.Cells(26 - L, 10) + xlsheet2.Cells(26 - L, 10) + xlsheet3.Cells(26 - L, 10)
xlsheet.Cells(L + 1, 10) = xlsheet1.Cells(27 - L, 10) + xlsheet2.Cells(27 - L, 10) + xlsheet3.Cells(27 - L, 10)
xlsheet.Cells(L + 2, 10) = xlsheet1.Cells(28 - L, 10) + xlsheet2.Cells(28 - L, 10) + xlsheet3.Cells(28 - L, 10)
xlsheet.Cells(L + 3, 10) = xlsheet1.Cells(29 - L, 10) + xlsheet2.Cells(29 - L, 10) + xlsheet3.Cells(29 - L, 10)
xlsheet.Cells(L + 4, 10) = xlsheet1.Cells(30 - L, 10) + xlsheet2.Cells(30 - L, 10) + xlsheet3.Cells(30 - L, 10)
xlsheet.Cells(L, 12) = xlsheet1.Cells(26 - L, 13) + xlsheet2.Cells(26 - L, 13) + xlsheet3.Cells(26 - L, 13)
xlsheet.Cells(L + 1, 12) = xlsheet1.Cells(27 - L, 13) + xlsheet2.Cells(27 - L, 13) + xlsheet3.Cells(27 - L, 13)
xlsheet.Cells(L + 2, 12) = xlsheet1.Cells(28 - L, 13) + xlsheet2.Cells(28 - L, 13) + xlsheet3.Cells(28 - L, 13)
xlsheet.Cells(L + 3, 12) = xlsheet1.Cells(29 - L, 13) + xlsheet2.Cells(29 - L, 13) + xlsheet3.Cells(29 - L, 13)
xlsheet.Cells(L + 4, 12) = xlsheet1.Cells(30 - L, 13) + xlsheet2.Cells(30 - L, 13) + xlsheet3.Cells(30 - L, 13)
xlbook.SaveAs ("d:\baobiaoku\ribao\" + Format(Date, "yyyy_mm_dd") + ".xls")

xlapp.Quit



Set xlapp = Nothing
Set xlbook = Nothing
Set xlsheet = Nothing
Set xlbook1 = Nothing
Set xlsheet1 = Nothing
Set xlbook2 = Nothing
Set xlsheet2 = Nothing
Set xlbook3 = Nothing
Set xlsheet3 = Nothing

End Sub
...全文
178 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lcj49997 2005-10-15
  • 打赏
  • 举报
回复
谢谢两位高手的答复!
gowowo 2005-10-06
  • 打赏
  • 举报
回复
我不能再现你的问题只能估计原因为:
(1)重复创建 Excel.Application对象 (2)Excel.Application对象不可视.
解决办法:
(1) 删除 “ Set xlapp = New Excel.Application ” 行
(2) 把 “ Set xlapp = CreateObject("Excel.Application") ” 改为
“ Set xlapp = CreateObject("Excel.Application"):xlapp.Application.Visible = True ”

faysky2 2005-10-06
  • 打赏
  • 举报
回复

你把捕捉异常的语句去掉,看错误出现在哪一语句上
【复现】并_离网风光互补制氢合成氨系统容量-调度优化分析(Matlab代码实现)内容概要:本文围绕“并_离网风光互补制氢合成氨系统容量-调度优化分析”的Matlab代码实现展开,旨在通过科研复现的方式,深入探讨风光互补发电系统与制氢、合成氨耦合的综合能源系统优化问题。文章聚焦于系统的容量配置与运行调度双重优化,利用Matlab平台构建数学模型,并可能结合YALMIP等优化工具包求解,以实现经济性、可靠性和环保性的多目标平衡。文中强调了科研工作中借鉴已有成果(复现)的重要性,并倡导研究者应具备严谨的逻辑思维、善于借力,并追求具有创新性的研究突破。; 适合人群:具备Matlab编程基础,从事新能源、综合能源系统、电力系统优化、氢能或化工过程等领域的科研人员和工程技术人员。; 使用场景及目标:① 复现并学习风光互补制氢合成氨系统的容量配置与调度优化模型;② 掌握利用Matlab进行综合能源系统建模与优化求解的方法;③ 获取可用于自身科研工作的代码资源和技术参考。; 阅读建议:读者应按文档提供的线索,从公众号“荔枝科研社”获取完整的代码和资料包,并结合所提供的网盘链接下载必要的软件(如YALMIP)进行实践。在学习过程中,应重点关注模型的构建思路、优化算法的选择与实现,并尝试在复现的基础上进行改进和创新。
内容概要:本文档围绕“【SCI一区复现】基于配电网韧性提升的应急移动电源预配置和动态调度(上)—MPS预配置(Matlab代码实现)”展开,系统介绍了通过Matlab代码实现应急移动电源(MPS)在配电网中的优化预配置方法,旨在提升电网在极端事件下的韧性。资源聚焦于日前与日内两个时间尺度的协同调度优化,涵盖模型构建、目标函数设计、约束条件处理及求解流程等关键技术环节,强调工程实用性与学术先进性,是高水平科研论文复现与教学实践的重要参考资料。; 适合人群:面向具备电力系统、电气工程、自动化等相关专业背景,熟悉Matlab/Simulink仿真平台,从事科研、工程设计或技术研发工作的硕士/博士研究生、高校教师及行业工程师。; 使用场景及目标:① 复现SCI一区高水平论文中的MPS预配置模型与优化算法;② 掌握配电网韧性评估与提升的关键技术路径;③ 深入理解多时间尺度下应急电源的优化调度机制;④ 为科研项目、学位论文或课题申报提供可验证的技术支撑与代码基础。; 阅读建议:本资源以Matlab代码为核心,建议读者结合文档中的理论分析与代码实现同步学习,重点关注建模逻辑与算法实现细节。推荐关注公众号“荔枝科研社”获取完整代码包及相关说明材料,以便开展仿真验证与结果分析。

2,506

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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