Excel VBA,Http.ResponseText運行時錯誤91

[英]Excel VBA, Http.ResponseText Runtime Error 91


Using Excel 2010 with VBA 7.0

將Excel 2010與VBA 7.0一起使用

Code in this video works until I get to 10:01. When execute the line

此視頻中的代碼一直有效,直到我到達10:01。執行時

Dim Resp As String: Resp = Http.ResponseText

Dim Resp As String:Resp = Http.ResponseText

I get a Runtime error 91 Object variable or With block variable not set.

我收到運行時錯誤91對象變量或未設置塊變量。

Question: How do I resolve this error message?

問題:如何解決此錯誤消息?

Private Sub btnRefresh_Click()
 Dim W As Worksheet: Set W = ActiveSheet
 Dim Last As Integer: Last = W.Range("A100").End(xlUp).Row
 Dim Symbols As String
 Dim i As Integer
 Dim URL As String
 Dim Http As WinHttpRequest
 If Last = 1 Then Exit Sub

 For i = 2 To Last
   Symbols = Symbols & W.Range("A" & i).Value & "+"
 Next i

 Symbols = Left(Symbols, Len(Symbols) - 1)
 URL = "http://finance.yahoo.com/d/quotes.csv?s=" & Symbols & "&f=snl1hg"
 Http = New WinHttpRequest
 Http.Open "GET", URL, False
 Http.Send

 Dim Resp As String: Resp = Http.ResponseText
  Debug.Print Symbols
 Debug.Print URL
End Sub

1 个解决方案

#1


2  

Two ways to solve this issue.

解決這個問題的兩種方法。


1) Set the reference to the Microsoft WinHttp 5.1 library (Tools->References toolbar in VBA) and fix the following lines of your code below:

1)設置對Microsoft WinHttp 5.1庫的引用(VBA中的Tools-> References工具欄)並修復以下代碼行:

Change

更改

Dim Http As WinHttpRequest     'Incorrect
Dim Http As New WinHttpRequest 'Correct

Delete

刪除

Http = New WinHttpRequest      'Incorrect

2) You can also do this with a separate object which will avoid the need to set the library reference in VBA:

2)您也可以使用單獨的對象來執行此操作,這將避免在VBA中設置庫引用:

Private Sub btnRefresh_Click()
  Dim W As Worksheet: Set W = ActiveSheet
  Dim Last As Integer: Last = W.Range("A100").End(xlUp).Row
  Dim Symbols As String
  Dim i As Integer
  Dim URL As String

  If Last = 1 Then Exit Sub

  For i = 2 To Last
    Symbols = Symbols & W.Range("A" & i).Value & "+"
  Next i

  Symbols = Left(Symbols, Len(Symbols) - 1)

  URL = "http://finance.yahoo.com/d/quotes.csv?s=" & Symbols & "&f=snl1hg"

  ' Create object here
  Dim HTTP As Object
  Set HTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
  HTTP.Open "GET", URL
  HTTP.Send

  'Test Response
  MsgBox HTTP.ResponseText

End Sub

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/08/12/72f342d2b5181e2e462766e994ae77ab.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com