Search code examples
javascriptexcelvbaexportgetelementsbytagname

Excel VBA For Each


I've created a macro that let me interface with a website and identify a certain button within the source, and use that to export the data to an Excel file. The below is my macro...the issue I'm finding below is in the comment. Please let me know if further specification is needed.

 Sub Scrape1()
 Dim Browser As InternetExplorer
 Dim Document As HTMLDocument
 Dim Elements As IHTMLElementCollection
 Dim Element As IHTMLElement
 Dim excelElement As IHTMLElement
 Dim objElement As Object

 Set Browser = New InternetExplorer
 Browser.Visible = True
 Browser.navigate "http://www.site.com"

 Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
 DoEvents
 Loop

 Set Document = Browser.Document

 Set Elements = Document.getElementById("ctl31_ctl06_ctl04_ctl00_Menu").
 getElementsByTagName("a")

 For Each Element In Elements

 'The object I'm looking to use has an InnerText of "Excel"
 'set objElement as the Element with the InnerText of "Excel" so that I can say 
 objElement.Click

 Next Element

 Set Document = Nothing
 Set Browser = Nothing
 End Sub

Solution

  • For Each Element In Elements
        'Debug.Print Element.innerText
        If Element.innerText = "Excel" Then 
            Element.Click
            Exit For
        End If
    
    Next Element