2014-10-31 92 views
4

我仍然試圖從網頁中獲取一些數據,我得到了一個解決方案,如何「grep」HTML文本,但現在我試圖從文本中分離出一些部分是在一個字符串中。Instr中的溢出錯誤

我想取出變量A和變量B之間的部分,並使用下面的代碼。

我總是得到一個Runtime error 6 Overflow,有人有一個想法爲什麼以及如何解決這個問題?

Sub Button1_Click() 

    Const HTTPREQUEST_PROXYSETTING_PROXY = 2  
    Dim oRequest As Object, pagetext, third As String, first, second As Integer 

    Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")  
    oRequest.Open "GET", "http://www.vodafone.de/privat/handys-tablets-tarife/smartphone-tarife.html" 

    oRequest.Send  
    pagetext = oRequest.ResponseText  

    first = InStr(pagetext, "window.phones") 
    second = InStr(first + 1, pagetext, "window.propositions") 
    third = Mid(pagetext, first + 1, second - first - 1) 

    MsgBox (third) 

End Sub 
+2

似乎你的'第三個'變量沒有被標註。如果你將'Option Explicit'作爲模塊的第一行粘貼,你可以幫助你避免很多運行時錯誤。 – 2014-10-31 10:54:05

+2

這不符合你的想法。 Dim oRequest As Object,pagetext,as As String,first,second As Integer'。 PageText和第一個被聲明爲變體。 – RubberDuck 2014-11-01 01:27:48

回答

6

變化從Integer的聲明Longsecond變量。

Integer類型可容納數高達32767和由表達式(154031)返回的值是大於。數據類型最多可以容納2,147,483,647個數字,因此它更適合您的特定情況。

通常,VBA現在將所有Integers轉換爲Long,因此默認情況下將變量調暗爲Long是一種更好的做法。

此外,您也應該在一行dim'ing多個變量時因爲

Dim oRequest As Object, pagetext, third As String, first, second As Integer 

first是一個Variantsecond被小心的Integer

它應該是

Dim oRequest As Object, pagetext, third As String, first as Long, second As Long