+ Reply to Thread
Results 1 to 3 of 3

HTML Select Multiple Values in Option Menu

Hybrid View

  1. #1
    Registered User
    Join Date
    02-10-2015
    Location
    LA
    MS-Off Ver
    2010
    Posts
    2

    HTML Select Multiple Values in Option Menu

    Hello Excel Forum,

    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!


    VBA:
    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
            
        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
    End Sub
    The HTML for the "State" ID can be seen below.
    <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>

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi, try this !


    PHP Code: 
    Sub Demo1()
        Const 
    WT 0.00002
        Dim oIE 
    As New InternetExplorer

        With oIE
            
    .Navigate "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple"
            
    .Visible True
            
    While .Busy Or .ReadyState 4:  DoEvents:  Wend

            With 
    .Document.all("iframeResult").contentWindow.Document.body.Children(0)
                .
    all(1).Selected True
                
    .all(4).Selected True
                 Application
    .Wait Now WT
                
    .submit
            End With

            Application
    .Wait Now WT
            
    .Quit
        End With

        Set oIE 
    Nothing
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

    But to pilot IE is the worst way ! Intermediate way is the worksheet function Query from Web.
    Better way is a request like a webbrowser uses …

  3. #3
    Registered User
    Join Date
    02-10-2015
    Location
    LA
    MS-Off Ver
    2010
    Posts
    2

    Re: HTML Select Multiple Values in Option Menu

    Thank you for the reply. I solved it using a different method. You can see my final code below.

    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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Select option in HTML drop down list
    By JohnsonJiang in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-15-2014, 03:28 PM
  2. Retrieve HTML <option> Values
    By danger_mouse in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-29-2013, 04:20 PM
  3. Replies: 1
    Last Post: 07-10-2012, 05:16 AM
  4. Replies: 3
    Last Post: 02-02-2012, 12:18 PM
  5. How can my macro select an option in a pull down menu?
    By Tim Richards in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2005, 07:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1