VBA InStr检查HTML

时间:2016-10-10 20:21:24

标签: html excel vba excel-vba

尝试使用VBA函数InStr检查HTML中的特定关键字,但不幸的是它没有返回正确的结果。这是VBA ......

With IE
    .Visible = True
    .navigate my_url4
End With

Do Until Not IE.Busy And IE.readyState = 4
    DoEvents
Loop

promo = IE.Document.getElementsByTagName("td") 
Debug.Print promo
If InStr(promo, "C8") = 0 Then
    ActiveSheet.Range("C10") = "Not Found"
Else
    ActiveSheet.Range("C10") = "Found"
End If    

这是我试图检查单元格C8中的值的HTML ...

<div style="overflow:auto; height=75%; scrollbar-face-color: #D6DFF7; scrollbar-track-color:white;">
<table border="1" width="100%" cellspacing="0" cellpadding="10" style="border-collapse: collapse" bordercolor="#111111">

    <tr>
        <td class='Header'>Customer History from  Monday, September 12, 2016 to Monday, October 10, 2016 </td> 
    </tr>

    <table border='1' width='100%' cellspacing='0' cellpadding='2'>
        <tr>
            <td class="Content" align="center" width="20%"><b>Promotion</b></td>
            <td class="Content" align="center" width="28%"><b>Issue Date</b></td>
        </tr>
    </table>                


    <table border='1' width='100%' cellspacing='0' cellpadding='2'>
    <tr>
        <td class='Content' width="20%"> October Offer  </td>
        <td class='Content' width="28%"> 10/10/2016 10:50:38 AM </td>
    </tr>
    </table>

    <table border='1' width='100%' cellspacing='0' cellpadding='2'>
    <tr>
        <td class='Content' width="20%"> Exclusive Online Promotion! </td>
        <td class='Content' width="28%"> 10/10/2016 10:50:32 AM </td>
    </tr>
    </table>

    <table border='1' width='100%' cellspacing='0' cellpadding='2'>
    <tr>
        <td class='Content' width="20%"> Exclusive October Offer  </td>
        <td class='Content' width="28%"> 10/10/2016 10:50:10 AM </td>
    </tr>
    </table>

 

无论我输入C8单元格,我都会在C10中得到相同的结果,这让我相信&#34; promo&#34;变量,因为InStr总是回来0.

任何见解都将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:1)

If InStr(promo, "C8") = 0 Then正在字符串变量promo中查找字符串“C8”。要查找单元格C8的值,您需要说

If InStr(promo, ActiveSheet.Range("C8").Value) = 0 Then