第二次导出到EXCEL时出错!!!

RIVERBASIN 2008-03-12 11:31:35
由于导出到EXCEL中的数据需要做一写排版工作,就直接把要排版的代码,用EXCEL录制宏的办法把代码粘贴到VB的代码区了。具体代码为:
With xlApp
.Range("A1:AA1").Merge
.Range("A1:AA1").FormulaR1C1 = "水库下泄沿程水温温度表"
.Range("A2:AA2").Select
.Selection.Merge
.ActiveCell.FormulaR1C1 = "测量日期:" & Year(arrvalues(1, 1)) & "-" & Month(arrvalues(1, 1)) & " 测量地点:" & Combo3.Text
.Range("A3:A4").Select
.Selection.Merge
.Selection.AutoFill Destination:=Range("A3:AA4"), Type:=xlFillDefault
.Range("B3:B4").Select
.ActiveCell.FormulaR1C1 = "21"
.Selection.AutoFill Destination:=Range("B3:D4"), Type:=xlFillDefault
.Range("B3:D4").Select
.Range("E3:E4").Select
.ActiveCell.FormulaR1C1 = "0"
.Selection.AutoFill Destination:=Range("E3:Y4"), Type:=xlFillDefault
.Range("E3:Y4").Select
.Range("Z3:Z4").Select
.ActiveCell.FormulaR1C1 = "Tmax"
With .ActiveCell.Characters(Start:=2, Length:=3).Font
.Subscript = True '设置下标
End With
.Range("AA3:AA4").Select
.ActiveCell.FormulaR1C1 = "Tmin"
With .ActiveCell.Characters(Start:=2, Length:=3).Font
.Subscript = True
End With
.Range("A5").Select
.ActiveCell.FormulaR1C1 = "1"
.Range("A6").Select
.ActiveCell.FormulaR1C1 = "2"
.Range("A5:A6").Select
.Selection.AutoFill Destination:=Range("A5:A14"), Type:=xlFillDefault
.Range("A5:A14").Select
'求平均
.Range("A15").Select
.ActiveCell.FormulaR1C1 = "第一侯平均"
.Range("B15").Select
.ActiveCell.FormulaR1C1 = "=AVERAGE(R[-10]C:R[-6]C)"
.Range("B15").Select
.Selection.AutoFill Destination:=Range("B15:Y15"), Type:=xlFillDefault
.Range("B15:Y15").Select
.Range("A16").Select
.ActiveCell.FormulaR1C1 = "第二侯平均"
.Range("B16").Select
.ActiveCell.FormulaR1C1 = "=AVERAGE(R[-6]C:R[-2]C)"
.Range("B16").Select
.Selection.AutoFill Destination:=Range("B16:Y16"), Type:=xlFillDefault
.Range("B15:Y15").Select
.Range("A17").Select
.ActiveCell.FormulaR1C1 = "上旬平均"
.Range("B17").Select
.ActiveCell.FormulaR1C1 = "=AVERAGE(R[-2]C:R[-1]C)"
.Range("B17").Select
.Selection.AutoFill Destination:=Range("B17:Y17"), Type:=xlFillDefault
.Range("B17:Y17").Select
'=============================================================================
.Range("A18").Select
.ActiveCell.FormulaR1C1 = "11"
.Range("A19").Select
.ActiveCell.FormulaR1C1 = "12"
.Range("A18:A19").Select
.Selection.AutoFill Destination:=Range("A18:A27"), Type:=xlFillDefault
.Range("A18:A27").Select
.Range("A28").Select
.ActiveCell.FormulaR1C1 = "第三侯平均"
.Range("B28").Select
.ActiveCell.FormulaR1C1 = "=AVERAGE(R[-10]C:R[-6]C)"
.Range("B28").Select
.Selection.AutoFill Destination:=Range("B28:Y28"), Type:=xlFillDefault
.Range("B28:Y28").Select
.Range("A29").Select
.ActiveCell.FormulaR1C1 = "第四侯平均"
.Range("B29").Select
.ActiveCell.FormulaR1C1 = "=AVERAGE(R[-6]C:R[-2]C)"
.Range("B29").Select
.Selection.AutoFill Destination:=Range("B29:Y29"), Type:=xlFillDefault
.Range("B29:Y29").Select
.Range("A30").Select
.ActiveCell.FormulaR1C1 = "中旬平均"
.Range("B30").Select
.ActiveCell.FormulaR1C1 = "=AVERAGE(R[-2]C:R[-1]C)"
.Range("B30").Select
.Selection.AutoFill Destination:=Range("B30:Y30"), Type:=xlFillDefault
.Range("B30:Y30").Select


Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing



每次出错的的地方都是EXCEL 自动填充功能上面出错,显示"实时错误1004,对象'Range'的方法'_Golbal'失败"!?
...全文
107 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
UltraBejing 2008-04-30
  • 打赏
  • 举报
回复
我也想了解,谢谢LZ.
longe329 2008-04-16
  • 打赏
  • 举报
回复
最后再加一句:xlbook.close试试
舉杯邀明月 2008-03-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 MiloSoft 的回复:]
如果第一次能够正常而第二次不正常,多半是第一次执行完EXCEL进程没有释放掉
[/Quote]

人家释放了的: ^_^

Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
沐NeMo 2008-03-23
  • 打赏
  • 举报
回复
对象'Range'的方法'_Golbal'失败"!?

本示例以工作表 Sheet1 上的单元格区域 A1:A2 为基础,对单元格区域 A1:A20 进行自动填充。运行本示例之前,请在单元格 A1 中键入 1,在单元格 A2 中键入 2。
Set sourceRange = Worksheets("Sheet1").Range("A1:A2")
Set fillRange = Worksheets("Sheet1").Range("A1:A20")
sourceRange.AutoFill Destination:=fillRange
自动填充前要先设置好数据源。
你在填充时候,确定
.Range("B17").Select
.ActiveCell.FormulaR1C1 = "=AVERAGE(R[-2]C:R[-1]C)"
.Range("B17").Select
.Selection.AutoFill Destination:=Range("B17:Y17"), Type:=xlFillDefault
.Range("B17:Y17").Select
里面有数值。
最好用程序设置或计算好数值后,直接填到excel里面。
MiloSoft 2008-03-23
  • 打赏
  • 举报
回复
如果第一次能够正常而第二次不正常,多半是第一次执行完EXCEL进程没有释放掉
舉杯邀明月 2008-03-23
  • 打赏
  • 举报
回复
我运行楼主的代码,没有出现异常……
RIVERBASIN 2008-03-19
  • 打赏
  • 举报
回复
怎么没有人帮我啊?
RIVERBASIN 2008-03-12
  • 打赏
  • 举报
回复
我只有30分了,大家帮帮忙

2,462

社区成员

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

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