这段写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
...全文
182 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
  • 打赏
  • 举报
回复

你把捕捉异常的语句去掉,看错误出现在哪一语句上
内容概要:本文提出一种基于鱼鹰优化算法(OOA)优化的CNN-BiGUR-Attention混合模型,用于提升短期风电功率预测的精度与稳定性,采用Matlab实现代码仿真。该模型融合卷积神经网络(CNN)提取输入数据的局部空间特征,利用双向门控循环单元(BiGUR)捕捉风速、功率等时间序列的前后向动态依赖关系,并引入注意力机制自适应强化关键时间步的特征权重,从而增强模型对非平稳风电数据的表征能力;进一步,采用OOA算法对模型超参数进行全局寻优,有效提升模型收敛速度与泛化性能。研究基于实际风电场历史数据开展实验验证,结果表明,该方法相较传统模型在预测精度、鲁棒性和误差抑制方面表现更优,适用于高比例可再生能源接入背景下的电力系统调度需求。; 适合人群:从事新能源发电预测、电力系统优化调度、智能算法与深度学习融合应用等方向的科研人员及工程技术人员,尤其适合具备Matlab编程能力、熟悉时间序列建模与深度学习框架的研究者。; 使用场景及目标:①实现风电场短期功率高精度预测,支撑电网安全稳定调度与能量管理;②为深度学习模型结构设计与智能优化算法联合调参提供实践范例;③推动人工智能技术在可再生能源预测、智能电网运行等领域的落地应用。; 阅读建议:建议结合提供的Matlab代码深入理解CNN-BiGUR-Attention网络架构搭建、注意力机制实现方式及OOA优化流程,重点关注数据预处理、模型训练与参数调优细节,可通过替换不同风电数据集进行对比实验,进一步掌握模型迁移能力与适应性。

2,506

社区成员

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

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