查找仅包含部分名称

时间:2015-09-10 20:34:40

标签: excel vba

我正在尝试使用Excel VBA操作Intranet内部的Internet Explorer,以便将Excel中的数据输入到网页中的相应数据字段中。为了使数据字段可用,必须勾选复选框。最初,当从未输入任何数据时,我认为命名机制总是checkBoxSum6为第一行,每行之间间隔为7。输入数据后不再是这种情况。

我现在需要找到一种方法来读取每个checkBoxSum [x]项目的整个文档,单击它们,并将它们的名称的数字片段存储到变量,可能是一个数组。我将存储它们以便使用它们来引用名为fVolumeEditSum [x]和fCommentsSumEdit [x]的输入框。

我查看了this之类的帖子。我还在Stack中搜索了似乎接近这个的解决方案,但是我发现的所有东西看起来都返回给定引用的数据值而不是引用本身的名称。

在学习网页的完整范围之前,我曾经迭代过这个问题的代码是:

Option Explicit
Sub OpenWebPage()

Dim IE As InternetExplorerMedium
Dim x As Integer, n As Integer
Dim firstDate As Date, secondDate As Date

n = DateDiff("ww", firstDate, secondDate)
x = 6

Do While n > 0
    IE.Document.all("checkBoxSum" & x).Click
    n = n - 1
    x = x + 7
Loop

End Sub

以下是HTML代码。它贯穿checkBoxSum的第二个版本。

<tr>
    <td colspan="2">
    <table cellpadding="0" cellspacing="0" border="0" class="dataTable" id="reportTable">
        <thead>
            <tr bgcolor="#E8E8E8">
                <td width="73" align="left"><span style="text-align:center; font-size:12px; white-space: nowrap"><b></b></span></td><!-- First icon -->
                <td width="73" align="left"><span style="text-align:center; font-size:12px; white-space: nowrap"><b></b></span></td><!-- Second icon -->
                <td width="73" align="left"><span style="text-align:center; font-size:12px; white-space: nowrap"><b>Update</b></span></td>
                <td width="73" align="left"><span style="text-align:center; font-size:12px; white-space: nowrap"><b>Date</b></span></td>
                <td width="73" align="left" style="display: none"><span style="text-align:center; font-size:12px; white-space: nowrap"><b>Business</b></span></td>
                <td width="73" align="left"><span style="text-align:center; font-size:12px; white-space: nowrap"><b>Volume</b></span></td>
                <td width="73" align="left"><span style="text-align:center; font-size:12px; white-space: nowrap"><b>Lock</b></span></td>
                <td width="73" align="left"><span style="text-align:center; font-size:12px; white-space: nowrap"><b>Comments&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></span></td>
                <td width="73" align="left" style="display: none"><span style="text-align:center; font-size:12px; white-space: nowrap"><b>Current</b></span></td>
                <td width="73" align="left" style="display: none"><span style="text-align:center; font-size:12px; white-space: nowrap"><b>ArrayRow</b></span></td>
                <td width="73" align="left" style="display: none"><span style="text-align:center; font-size:12px; white-space: nowrap"><b>RowType</b></span></td>
            </tr>
        </thead>
        <tbody>














             <!-- Counter starts at 1 array at 0 -->
             <!-- Counter starts at 1 array at 0 -->
             <!-- Counter starts at 1 array at 0 -->








                        <!-- current_N -->
                            <tr style="display: none" class="main"></tr><tr id="mainRow20" class="main" myindex="20">
                            <td>
                                <div id="iconShowA20" class="ui-icon ui-icon-plusthick" onclick="showExpand('20','20')" ;=""></div>
                                <div id="iconShowB20" class="ui-icon ui-icon-minusthick" style="display: none" onclick="hideExpand('20','20')" ;=""></div>
                            </td>
                            <td align="left"></td><!-- Second icon -->
                            <td align="left">
                            <input type="checkbox" name="checkBoxSum20" id="checkBoxSum20" onclick="checkItWeek('20','20')" ;=""></td>
                            <td align="left"><div id="divWeekDTSum20"><b>10/12/2015</b></div></td>
                            <td align="left" style="display: none"></td>
                            <td align="left" style="display: none"><div id="divBusinessSum20">PL_LEGACY</div></td>
                                <td align="left" id="TDfVolumeSum20"><b>0</b></td>
                                <td align="left" id="TDfVolumeSumEdit20" style="display: none"><b><input type="text" class="allowedValues" id="fVolumeEditSum20" name="fVolumeEditSum20" value="0" onkeyup="fVolumeEditKeyUpSum(20)" ;="" size="5"> </b></td>
                            <td align="left">
                                <input type="checkbox" id="lockSum20" name="lockSum20" onclick="fVolumeEditKeyUpSum(20)" ;="">
                            </td>
                                <td id="TDfCommentsSum20" align="left"><b></b></td>
                                <td id="TDfCommentsSumEdit20" style="display: none" align="left"><input type="text" onkeyup="fVolumeEditKeyUpSum(20)" ;="" id="fCommentsSumEdit20" name="fCommentsSumEdit20"></td>
                            <td align="left" style="display: none"><b></b></td>
                            <td align="left" style="display: none"><input type="text" id="arrayStoreSum20" name="arrayStoreSum20" readonly="readonly"></td>
                            <td align="left" style="display: none"><div>MainSum</div></td>
                        </tr><tr class="expandableDay" id="rowShow2" style="display: none" bgcolor="#EBEEF2" title="Creator : LUSCHR 
Create Date : 09/10/2015">
                                    <td align="left"></td><!-- First icon -->
                                    <td>
                                        <div id="iconShowA_N2" class="ui-icon ui-icon-plusthick" onclick="showExpandSub('2','2')" ;=""></div>
                                        <div id="iconShowB_N2" class="ui-icon ui-icon-minusthick" style="display: none" onclick="showExpandSub('2','2')" ;=""></div>
                                    </td>
                                    <td align="left">

                                            <input type="checkbox" name="checkBox2" id="checkBox2" onclick="checkIt(2);">       

                                    </td>
                                    <td bgcolor="#EBEEF2" align="left" style="display: none"><div id="divWeekDT2">10/12/2015</div></td>
                                    <td bgcolor="#EBEEF2" align="left"><div id="divActivityDT2">10/12/2015</div></td>
                                    <td bgcolor="#EBEEF2" align="left" style="display: none"><div id="divBusiness2">PL_LEGACY</div></td>
                                        <td id="TDfVolume2" bgcolor="#EBEEF2" align="left">0</td>
                                        <td id="TDfVolumeEdit2" style="display: none" bgcolor="#EBEEF2" align="left"><input type="text" class="allowedValues" onkeyup="fVolumeEditKeyUp(2)" ;="" name="fVolumeEdit2" id="fVolumeEdit2" size="5" value="0"> </td>
                                    <td bgcolor="#EBEEF2" align="left"><div id="divLockFlag2">N</div></td>
                                        <td id="TDfComments2" bgcolor="#EBEEF2" align="left"> </td>
                                        <td id="TDfCommentsEdit2" style="display: none" bgcolor="#EBEEF2" align="left"><input type="text" onkeyup="fVolumeEditKeyUp(2)" ;="" id="fCommentsEdit2" name="fCommentsEdit2"></td>
                                    <td bgcolor="#EBEEF2" align="left" style="display: none">Y</td>
                                    <td bgcolor="#EBEEF2" align="left" style="display: none"><input type="text" id="arrayStoreRow2" name="arrayStoreRow2" readonly="readonly"></td>
                                    <td bgcolor="#EBEEF2" align="left" style="display: none"><div>current_Y_N</div></td>
                                </tr><tr class="current_N" id="current_N1" ncnt="1" style="display: none" bgcolor="#F6F6F6" title="Creator : LUSCHR 
Create Date : 09/10/2015">
                                <td align="left"></td><!-- First icon -->
                                <td align="left"></td><!-- Second icon -->
                                <td align="left"></td><!-- Checkbox -->
                                <td bgcolor="#F6F6F6" align="left" style="display: none"><div id="divWeekDT1">10/12/2015</div></td>
                                <td bgcolor="#F6F6F6" align="left"><div id="divActivityDT1">10/12/2015</div></td>
                                <td bgcolor="#F6F6F6" align="left" style="display: none"><div id="divBusiness1">PL_LEGACY</div></td>
                                    <td id="TDfVolume1" bgcolor="#F6F6F6" align="left">.19</td>
                                    <td id="TDfVolumeEdit1" style="display: none" bgcolor="#F6F6F6" align="left"><input type="text" class="allowedValues" onkeyup="fVolumeEditKeyUp(1)" ;="" name="fVolumeEdit1" id="fVolumeEdit1" size="5" value=".19"> </td>
                                <td bgcolor="#F6F6F6" align="left"><div id="divLockFlag1">N</div></td>
                                    <td id="TDfComments1" bgcolor="#F6F6F6" align="left"> </td>
                                    <td id="TDfCommentsEdit1" style="display: none" bgcolor="#EBEEF2" align="left"><input type="text" onkeyup="fVolumeEditKeyUp(1)" ;="" id="fCommentsEdit1" name="fCommentsEdit1"></td>
                                <td bgcolor="#F6F6F6" align="left" style="display: none">N</td>
                                <td bgcolor="#F6F6F6" align="left" style="display: none"><input type="text" id="arrayStoreRow1" name="arrayStoreRow1" readonly="readonly"></td>
                                <td bgcolor="#F6F6F6" align="left" style="display: none"><div>current_N</div></td>
                            </tr><tr class="current_N" id="current_N0" ncnt="0" style="display: none" bgcolor="#F6F6F6" title="Creator : ADMIN 
Create Date : 12/06/2012">
                                <td align="left"></td><!-- First icon -->
                                <td align="left"></td><!-- Second icon -->
                                <td align="left"></td><!-- Checkbox -->
                                <td bgcolor="#F6F6F6" align="left" style="display: none"><div id="divWeekDT0">10/12/2015</div></td>
                                <td bgcolor="#F6F6F6" align="left"><div id="divActivityDT0">10/12/2015</div></td>
                                <td bgcolor="#F6F6F6" align="left" style="display: none"><div id="divBusiness0">PL_LEGACY</div></td>
                                    <td id="TDfVolume0" bgcolor="#F6F6F6" align="left">0</td>
                                    <td id="TDfVolumeEdit0" style="display: none" bgcolor="#F6F6F6" align="left"><input type="text" class="allowedValues" onkeyup="fVolumeEditKeyUp(0)" ;="" name="fVolumeEdit0" id="fVolumeEdit0" size="5" value="0"> </td>
                                <td bgcolor="#F6F6F6" align="left"><div id="divLockFlag0">N</div></td>
                                    <td id="TDfComments0" bgcolor="#F6F6F6" align="left">place holder
</td>
                                    <td id="TDfCommentsEdit0" style="display: none" bgcolor="#EBEEF2" align="left"><input type="text" onkeyup="fVolumeEditKeyUp(0)" ;="" id="fCommentsEdit0" name="fCommentsEdit0"></td>
                                <td bgcolor="#F6F6F6" align="left" style="display: none">N</td>
                                <td bgcolor="#F6F6F6" align="left" style="display: none"><input type="text" id="arrayStoreRow0" name="arrayStoreRow0" readonly="readonly"></td>
                                <td bgcolor="#F6F6F6" align="left" style="display: none"><div>current_N</div></td>
                            </tr>

                        <!-- current_N -->

             <!-- Counter starts at 1 array at 0 -->
             <!-- Counter starts at 1 array at 0 -->
             <!-- Counter starts at 1 array at 0 -->










                                <!-- THERE ARE NO DUPLICATES FOR THIS DAY -->
                                <tr id="mainRow48" class="main" myindex="48">
                            <td>
                                <div id="iconShowA48" class="ui-icon ui-icon-plusthick" onclick="showExpand('6','48')" ;=""></div>
                                <div id="iconShowB48" class="ui-icon ui-icon-minusthick" style="display: none" onclick="hideExpand('6','48')" ;=""></div>
                            </td>
                            <td align="left"></td><!-- Second icon -->
                            <td align="left">
                            <input type="checkbox" name="checkBoxSum48" id="checkBoxSum48" onclick="checkItWeek('6','48')" ;=""></td>
                            <td align="left"><div id="divWeekDTSum48"><b>10/26/2015</b></div></td>
                            <td align="left" style="display: none"></td>
                            <td align="left" style="display: none"><div id="divBusinessSum48">PL_LEGACY</div></td>
                                <td align="left" id="TDfVolumeSum48"><b>0</b></td>
                                <td align="left" id="TDfVolumeSumEdit48" style="display: none"><b><input type="text" class="allowedValues" id="fVolumeEditSum48" name="fVolumeEditSum48" value="0" onkeyup="fVolumeEditKeyUpSum(48)" ;="" size="5"> </b></td>
                            <td align="left">
                                <input type="checkbox" id="lockSum48" name="lockSum48" onclick="fVolumeEditKeyUpSum(48)" ;="">
                            </td>

更新*我喜欢迭代的想法。在代码中,我计算了要单击的复选框的数量,它是Do While循环的一部分。是否可以使用On Error GoTo跳过n-n-1行,并增加x值?如果我可以让它工作,我应该能够完成整个任务。我的第一次尝试仍然会出现运行时错误&#39; 91&#39;

Do While n > 0
    On Error GoTo Iterate
    IE.Document.all("checkBoxSum" & x).Click

    n = n - 1
Iterate:        x = x + 7
Loop

1 个答案:

答案 0 :(得分:0)

您还可以通过querySelector的{​​{1}}方法应用CSS选择器来匹配元素,并返回document;以及一个辅助函数来解析数字的关联元素的HTML。

大纲代码:

nodeList

CSS选择器:

Option Explicit
Public Sub GetInfoSetChecks()
    'other code to browse to page etc.
    Dim aNodeList As Object, i As Long
    Set aNodeList = ie.document.querySelectorAll("input[id^='checkboxSum']")

    For i = 0 To aNodeList.Length - 1
        aNodeList.item(i).Checked = True
        ' Debug.Print aNodeList(i).Check = True ''<== sometimes this syntax instead
        Debug.Print GetCheckBoxNumber(aNodeList.item(i).outerHTML)
        'Debug.Print GetCheckBoxNumber(aNodeList(i).outerHTML)
    Next
    'Other code
End Sub

Public Function GetCheckBoxNumber(ByVal inputString As String) As Long
    GetCheckBoxNumber = Split(Split(inputString, "checkBoxSum")(1), Chr$(34))(0)
End Function

这表示带有input[id^='checkboxSum'] 标签的元素包含以{{1}开始的属性input


关于HTML示例的CSS查询:

CSS query