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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16975506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:30:59  来源:igfitidea点击:

how to download source code from a website with vba ?

vba

提问by demonslayer1

I wrote a macro which get the source data from a website and downloads , copy and paste each line in the active worksheet. When I move from the line Lines2(k - 1).Copy to the next line , I get Run time error 424 Object is required.

我写了一个宏,它从网站获取源数据并下载、复制和粘贴活动工作表中的每一行。当我从 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

回答by Santosh

Try below code

试试下面的代码

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