VBA Excel VBA Set webpage drop down selection

sirjaymz

New Member
Joined
Feb 15, 2022
Messages
5
Office Version
  1. 2010
Platform
  1. Windows
I have looked and tried so many different methods to just perform a simple selection.

Looking to set the dropdown to 2021. It defaults to 2022.

VBA Code:
Sub Get_Info
       Dim sBrowserHwnd
       Dim sURL As String
       Dim sURLLoc As String
       Dim Element As Object ' HTMLButtonElement
       Dim btnInput As Object ' MSHTML.HTMLInputElement

       sURL = "https://public.hcad.org/records/Real.asp?search=acct"

       Set Browser = CreateObject("InternetExplorer.Application")
       Browser.Visible = True 
       Browser.navigate sURL                                          ' navigate to page
                WaitForBrowser Browser, 3  
       Set HTMLDoc = Browser.document
       Set varSearchYear = HTMLDoc.getElementsByName("TaxYear")
            For Each elem In varSearchYear.getElementsByTagName("option")
                If elem.Value = "2021" Then
                    elem.Selected = True
                    Exit For
                End If
            
            Next elem

' Tried this one as well
          'HTMLDoc.getElementsByTagName("select").Value = "2021"
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Tried this as well.

VBA Code:
            Set HTMLDoc = Browser.document
                        
            Set varSearchYear = HTMLDoc.getElementsByName("TaxYear")
             
            'loop through all 'searchSearchYear' elements and find the one with a specific value
            For Each dropOption In varSearchYear.getElementsByTagName("option")
                If dropOption.innerText = "2021" Then
                    varSearchYear.Value = dropOption.Value
                    Exit For
                End If
            
            Next dropOption
 
Upvote 0
ok.. Tried this too...
Really getting frustrated....uggg

VBA Code:
            Set HTMLDoc = Browser.document
                        
            Set varSearchYear = HTMLDoc.getElementsByName("TaxYear")
             
            'loop through all 'searchSearchYear' elements and find the one with a specific value
            For Each dropOption In varSearchYear.getElementsByTagName("select")(0).getElementsByTagName("option")
                If dropOption.innerText = "2021" Then
                    dropOption.Selected = True
                    'varSearchYear.Value = dropOption.Value
                    Exit For
                End If
            
            Next dropOption
 
Upvote 0
VBA Code:
    HTMLdoc.getElementsByName("TaxYear")(0).Value = "2022"
Thanks for the response.. Not sure I understood exactly, but I got the Run=Time error '13: Type mismatch now...

VBA Code:
            Set HTMLDoc = Browser.document
            
            Set varSearchYear = HTMLDoc.getElementsByName("TaxYear")(0).Value = "2022"
            For Each elem In varSearchYear.getElementsByTagName("option")
                If elem.Value = "2021" Then
                    elem.Selected = True
                    Exit For
                End If
 
Upvote 0
Got it... yay..

VBA Code:
            Set HTMLDoc = Browser.document
            
            Set varSearchYear = HTMLDoc.getElementsByName("TaxYear")(0)
            For Each elem In varSearchYear.getElementsByTagName("option")
                If elem.Value = "2021" Then
                    elem.Selected = True
                    Exit For
                End If
            
            Next elem
 
Upvote 0
Solution

Forum statistics

Threads
1,223,912
Messages
6,175,348
Members
452,638
Latest member
Oluwabukunmi

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