当前位置:编程学习 > VB >>

求高手指点,调用子程序,向已有的excel添加数据,为什么会出现对象‘cells’的方法‘—worksheet’失败

[size=24px]子程序为:
Set xlapp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlbook = xlapp.Workbooks.Open("C:\Documents and Settings\Administrator\桌面\逆温层\20000107.xls") '打开已经存在的test.xls工件簿文件
xlapp.Visible = True '设置EXCEL对象可见(或不可见)
Set xlsheet = xlbook.Worksheets(1) '设置活动工作表''
Dim q%
Workbooks.Open ("C:\Documents and Settings\Administrator\桌面\逆温层\20000107.xls ")
With ActiveSheet
  q = .UsedRange.Rows.Count '行数
End With
Close #1
'检测txt文件内有多少条记录
  Open Txtwenj.Text For Input As #1
  m = 0
  While Not EOF(1)
  Input #1, a
  m = m + 1
  Wend[/size]  Close #1

  Open Txtwenj.Text For Input As #1
  For n = 1 To m
  Line Input #1, a
  m1 = n
    
  If m1 = 1 Then
  If Mid(a, 3, 1) = 0 Then
  a1 = Mid(a, 4, 1)
  xlsheet.Cells(q, 1) = a1
  Else
  a1 = Mid(a, 3, 2)
  xlsheet.Cells(q, 1) = a1
  End If 
--------------------编程问答-------------------- 把Dim q%
Workbooks.Open ("C:\Documents and Settings\Administrator\桌面\逆温层\20000107.xls ")
With ActiveSheet
中间的workbooks.open这一句注释掉,你已经打开了,不需要重复打开
这样就解决了你的cells的方法worksheet失败问题
但是,你的代码还有问题
While Not EOF(1)
  Input #1, a
  m = m + 1
  Wend[/size] Close #1

  Open Txtwenj.Text For Input As #1
  For n = 1 To m
  Line Input #1, a
  m1 = n


你的m在第一个循环中记录文件行数为m,但是这个m的值为文件总行数+1
第二个循环for n=1 to m 将会越界. --------------------编程问答-------------------- 非常感谢指出了程序运行的错误,
我刚注册,不知道有没有分值可以赠送给您,还请以后多多赐教 --------------------编程问答-------------------- 我去..页面上这不是有"管理菜单" "结贴" "发帖" "回复"..你点结贴就能给分



-----------------------------
女子女子穴习,二人二人向下
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,