错误1004 - 将值写入单元格

时间:2016-05-16 16:49:07

标签: excel vba excel-vba

我的代码很简单,但似乎无法摆脱这个错误。我已经尝试将字符串分配给变量然后分配它,取消保护表单,并将代码放入新的子函数中。错误发生在第二个子Analyze()中。

Enum READYSTATE
    READYSTATE_UNINTIALIZED = 0
    READYSTATE_LOADING = 1
    READYSTATE_LOADED = 2
    READYSTATE_INTERACTIVE = 3
    READYSTATE_COMPLETE = 4
End Enum

Dim html As HTMLDocument

Sub Testing()
    Dim ie As InternetExplorer

    Set ie = New InternetExplorer
    ie.Visible = True
    ie.navigate "http://www.realclearpolitics.com/epolls/latest_polls/elections/"

    Do While ie.READYSTATE <> READYSTATE_COMPLETE
        Application.StatusBar = "Trying to reach RealClearPolitics.com"
        DoEvents

    Loop
    Set html = ie.document
    MsgBox html.DocumentElement.outerHTML
    Set ie = Nothing
    Application.StatusBar = " "

    Call Analyze

End Sub

Sub Analyze()
    Dim polldate As String
    polldate = "date"

    Worksheets("Sheet1").Activate

    Cells.Clear

    Worksheets("Sheet1").Cells(0, 0).Value = "Date"
    Worksheets("Sheet1").Cells(0, 1).Value = "Poll"
    Worksheets("Sheet1").Cells(0, 2).Value = "Page"

End Sub

2 个答案:

答案 0 :(得分:4)

Cells()的索引必须以 1 开头,而不是 0

答案 1 :(得分:0)

在使用Call Analyze破坏InternetExplorer对象之前放置Set ie = Nothing。将html设置为ie对象的文档,然后进行破坏,即不会留下任何文档。