HTML Select Multiple Values in Option Menu

San_K

New Member
Joined
Feb 18, 2015
Messages
2
Hello,

This is my first post. I'm very new to HTML and have a little bit of experience with VBA. I am trying to navigate through a webpage so that I can eventually grab the table from the page.

There is a multi-select dropdown menu on the page similar to the drop down menu in the following link.
http://www.w3schools.com/tags/tryit....elect_multiple

I need to be able to select multiple values in the drop-down menus and then submit the search. There are multiple drop-down menus to select from. One drop menu refers to "State" and the other to "Chain". Currently, I am able to select one drop-down menu item and submit the search which can be seen in the code below.

Help is greatly appreciated! Thanks!



Code:
[COLOR=#333333]Sub Navigate_Webpage()[/COLOR]
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit;">
Const cURL = "website"
Const cUsername = "username"
Const cPassword = "password"
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim UserNameInputBox As HTMLInputElement
Dim PasswordInputBox As HTMLInputElement
        
    Set IE = New InternetExplorer
    
        IE.Visible = True
        IE.Navigate cURL
        Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    Set doc = IE.Document
    
    Set LoginForm = doc.forms(0)
  
    Set UserNameInputBox = LoginForm.elements("login")
        UserNameInputBox.Value = cUsername
    
    Set PasswordInputBox = LoginForm.elements("password")
        PasswordInputBox.Value = cPassword
    
    IE.Document.forms(0).submit
        Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
        
    
    IE.Navigate "Navigate to page on website once logged in"
        Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    Set doc = IE.Document
    
        With doc
               With .getElementById("State")
                    .Value = "OR"
                    .Focus
                    .FireEvent "onchange"
                End With
               
                With .getElementById("Chain")
                    .Value = "LV"
                    .Focus
                    .FireEvent "onchange"
                End With
              
                Set tags = IE.Document.getElementsByTagName("Input")
                For Each tagx In tags
                    If tagx.Value = "Search" Then
                        tagx.Click
                        Exit For
                    End If
                Next
        End With </code>[COLOR=#333333]End Sub[/COLOR]


The HTML for the "State" ID can be seen below. It allows the user to manually input multiple values by using ctrl+click. Please right-click and use inspect element on the HTML to view the HTML code. I can't seem to figure out how to post the html code into my post.


<select name="State" id="State" size="7" multiple="true"> <option value="">-- All --</option> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AB">Alberta</option> <option value="AZ">Arizona</option> <option value="AR">Arkansas</option> <option value="BC">British Columbia</option> </select>


Thank you for your time!

 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try this (it selects the 2nd and 5th options).
Code:
    Dim selectElement As HTMLSelectElement
    Set selectElement = doc.getElementById("State")
    selectElement.Options(1).Selected = True
    selectElement.Options(4).Selected = True
If you want to select the options by their visible text or underlying Value attribute then you have to loop through the Options array looking for the required Options(n).Text or Options(n).Value and set the Selected property for the found Options(n) index as above.
 
Upvote 0
Thank you for the help John!

Here's what my final code turned out to be.

Code:
Sub Navigate_Webpage()

Const cURL = "website"
Const cUsername = "username"
Const cPassword = "password"
Dim IE As InternetExplorer
Dim doc As HTMLDocument
Dim LoginForm As HTMLFormElement
Dim UserNameInputBox As HTMLInputElement
Dim PasswordInputBox As HTMLInputElement
Dim selectelementstate As HTMLSelectElement
        
    Set IE = New InternetExplorer
    
        IE.Visible = True
        IE.Navigate cURL
        Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    Set doc = IE.Document
    
    Set LoginForm = doc.forms(0)
  
    Set UserNameInputBox = LoginForm.elements("login")
        UserNameInputBox.Value = cUsername
    
    Set PasswordInputBox = LoginForm.elements("password")
        PasswordInputBox.Value = cPassword
    
    IE.Document.forms(0).submit
        Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
        
    
    IE.Navigate "Navigate to page on website once logged in"
        Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    Set doc = IE.Document
    Set selectelementstate = doc.getElementById("State")
    
   n = 0
   For Each selectelement In selectelementstate
        If selectelementstate.Options(n).Value = "CA" Or _
            selectelementstate.Options(n).Value = "OR" Or _
            selectelementstate.Options(n).Value = "WA" Or _
            selectelementstate.Options(n).Value = "NV" Or _
            selectelementstate.Options(n).Value = "WY" Or _
            selectelementstate.Options(n).Value = "MT" Or _
            selectelementstate.Options(n).Value = "AZ" Or _
            selectelementstate.Options(n).Value = "ID" Or _
            selectelementstate.Options(n).Value = "CO" Or _
            selectelementstate.Options(n).Value = "NM" Or _
            selectelementstate.Options(n).Value = "UT" Then
            
            selectelementstate.Options(n).Selected = True
        
        End If
    n = n + 1
   Next
    
                For Each tagx In tags
                    If tagx.Value = "Search" Then
                        tagx.Click
                        Exit For
                    End If
                Next
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,316
Messages
6,124,228
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top