如何从vba网站下载源代码?

时间:2013-06-07 02:51:56

标签: vba

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

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

1 个答案:

答案 0 :(得分: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