2

我编写了一个宏,它从网站获取源数据并下载、复制和粘贴活动工作表中的每一行。当我从 Lines2(k - 1).Copy 移动到下一行时,我得到运行时错误 424 Object is required。

Private Sub UserForm_Click()
Dim URL2 As String: URL2 = "http://finance.yahoo.com/"
' to get data from the url we need to creat a win Http object_
' tools > references > select Windows Win Http Services 5.1

Dim Http2 As New WinHttpRequest
'open the url
Http2.Open "GET", URL2, False
'Debug.Print s
'Debug.Print URL

' send request
Http2.Send
MsgBox Http2.ResponseText
Debug.Print s
'Debug.Print Http2
Debug.Print URL2
Dim Resp As String: Resp = Http2.ResponseText
Dim Lines2 As Variant: Lines2 = Split(Resp, vbNewLine)
Debug.Print Lines2(0)

f = UBound(Lines2)

For k = 1 To f + 1
Lines2(k - 1).Copy
Range(10 + k, 1).Select
ActiveSheet.Paste
Next k

End Sub
4

1 回答 1

4

试试下面的代码

Private Sub UserForm_Click()
    Dim URL2 As String: URL2 = "http://finance.yahoo.com/"
    ' to get data from the url we need to creat a win Http object_
    ' tools > references > select Windows Win Http Services 5.1

    Dim Http2 As New WinHttpRequest
    'open the url
    Http2.Open "GET", URL2, False
    'Debug.Print s
    'Debug.Print URL

    ' send request
    Http2.Send
    MsgBox Http2.ResponseText
    Debug.Print s
    'Debug.Print Http2
    Debug.Print URL2
    Dim Resp As String: Resp = Http2.ResponseText
    Dim Lines2 As Variant: Lines2 = Split(Resp, vbNewLine)
    Debug.Print Lines2(0)

    For k = LBound(Lines2) To UBound(Lines2)
        ActiveSheet.Cells(10 + k, 1).Value = Lines2(k)
    Next k
End Sub
于 2013-06-07T03:13:47.853 回答