VBA 代码如何 select 从下拉列表中选择一个选项

VBA code how select an option from drop down

我的 VBA 代码有问题。我想 select 并更改选项值

<div class="pull-left">
        <span data-bind="text: allTasks.itemsFromIndex">1</span> To
        <span data-bind="text: allTasks.itemsToIndex">10</span> Of
        <span data-bind="text: allTasks.totalCount">39</span> All Pending Tasks
    </div>
        Show
        <select data-bind="value: allTasks.pageSize">
            <option value="5">5</option>
            <option value="10">10</option>
            <option value="20">20</option>
            <option value="50">50</option>
            <option value="100">100</option>
        </select>
        Per Page

根据您的描述,您似乎想 select 通过使用 VBA.

使 IE 浏览器自动化来实现下拉选项

我们可以看到下拉元素没有 ID 或名称来访问它。所以在这里我们可以尝试使用其 data-bind 属性值找到该特定元素。

VBA代码:

Sub demo()

    Dim ie As Object
    Dim selectElement As HTMLSelectElement
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.navigate "https://Your_Site_URL_here..."  'Modify the URL here...

    Do While ie.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
        
    Set selectElement = ie.document.querySelector("select[data-bind='value: allTasks.pageSize']")
    selectElement.Options(3).Selected = True  ' Note that index value starts from 0...
   
    'ie.Quit
End Sub

输出:

另外,您可以根据自己的需求尝试修改代码示例。