Excel VBA InStr function

2019-09-21 16:47发布

Why does my InStr function return 0 when it clearly is part of the HTML string?

I cannot post this without more text.I cannot post this without more text.I cannot post this without more text.I cannot post this without more text.I cannot post this without more text.I cannot post this without more text.

Sub ImportBPlans()
    Dim BPlan As String, FullHTML As String, URL1 As String, Cut1 As String, T1 As String
    Dim FO As String, LO As String, Other1 As Long

    For i = 2 To LastRow

        T1 = WB1.Cells(i, 1).Value

        URL1 = "http://finance.yahoo.com/q/pr?s=" + T1 + "+Profile"
        FullHTML = GetHTML(URL1)
        BPlan = "&nbsp;</th></tr></table><p>"
        x = Len(FullHTML)
        FO = InStr(FullHTML, BPlan, CompareMethod.Text) + Len(BPlan)
        LO = InStr(FO, FullHTML, "<")
        Cut1 = Left(FullHTML, LO)
        Cut1 = Right(Cut1, FO - LO)
        WB5.Cells(i, 1).Value = Cut1

    Next i


End Sub

Function GetHTML(URL As String) As String
    Dim HTML As String
    Dim htmlBDY As New HTMLDocument
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .Send
        htmlBDY.body.innerHTML = .responseText
        GetHTML = htmlBDY.body.outerHTML
    End With
End Function

2条回答
SAY GOODBYE
2楼-- · 2019-09-21 17:26

FO and LO are defined as string. it should be integer as Instr returns integer. Get it corrected.

Public Shared Function InStr(_
   ByVal String1 As String, _
   ByVal String2 As String, _
   Optional ByVal Compare As CompareMethod _
) As Integer
查看更多
▲ chillily
3楼-- · 2019-09-21 17:40

Try this:

    FO = InStr(1, FullHTML, BPlan, vbTextCompare) + Len(BPlan)
查看更多
登录 后发表回答