Instr()中的溢出错误

时间:2014-10-31 10:02:09

标签: excel vba

我仍在尝试从网页中获取一些数据,我得到了一个解决方法,如何“grep”HTML文本,但现在我正在尝试将这些文本中的某些部分分开。< / p>

我想取出变量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

1 个答案:

答案 0 :(得分:8)

Integer变量的声明从Long更改为second

Integer类型可以包含最多32,767的数字,并且表达式( 154031 )返回的值大于该值。 Long数据类型可以容纳最多2,147,483,647的数字,因此它更适合您的特定情况。

通常情况下,VBA最近会将所有Integers转换为Long,因此默认情况下将变量调暗为Long会更好。

此外,在一行中调暗多个变量时也应该小心,因为

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

firstVariantsecondInteger

应该是

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