51Testing软件测试论坛

标题: 如何读取Excel文档数据问题 [打印本页]

作者: bb64844866    时间: 2008-5-21 08:53
标题: 如何读取Excel文档数据问题
Sub Main
    Dim Result As Integer
    dim excel as Object
    dim book as Object
    dim worksheet as Object
    dim s_name as String
    dim s_pass as String
    dim count as Integer
   
    Set excel = GetObject(,"excel.application")
    if(excel Is Nothing) then
        Set excel = CreateObject("excel.application")
        if(excel Is Nothing) then
            MsgBox "Couldn't find Excel!"
            Exit Sub
        End if
    End if
    Set book = excel.Workbooks.Open("c:\Your Book1.xls")
    Set worksheet = book.Worksheet("Sheet1")   
    For count=1 To 2
    s_name = worksheet.Cells(1,count).value
    s_pass = worksheet.Cells(2,count).value
    Next
end sub
请问各位有经验人事,为什么单步调试到红色语句(Set worksheet = book.Worksheet("Sheet1"))时,提示"no such property or method"... ...难道是语法不对吗?不太明白。

[ 本帖最后由 bb64844866 于 2008-5-21 08:55 编辑 ]
作者: 村上舞!舞!舞    时间: 2008-5-26 16:33
把你的:Set worksheet = book.Worksheet("Sheet1")    这一句改成
Set worksheet = book.Worksheets("Sheet1") 这个吧
作者: bb64844866    时间: 2008-5-28 15:35
标题: 问题已解决,感谢回复者!
漏掉了个字母s,惭愧...感谢二楼朋友。
我想再问下,如果是向Excel中写入数据时,为什么只能写入到第一列呢?如何写到其它列?请指教!
代码如下:
Dim x as Integer
dim book as object
dim worksheet as object
dim excel as object
dim a1
   Open "C:\aa.xls" for output as #1
   For x=1 to 10
      Write #1, x
   Next
   Close #1




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2