Search code examples
vbainternet-explorer

VBA code how select an option from drop down


I have a problem with the VBA code. I want to select and change the Option value

<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

Solution

  • From your description, it looks like you want to select a dropdown option by automating the IE browser using VBA.

    We can see that the dropdown element does not have an ID or Name to access it. so here we can try to find that specific element using its data-bind attribute value.

    VBA code:

    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
    

    Output:

    enter image description here

    Further, you can try to modify the code example as per your own requirements.