I want to download some data from a webpage (http://www.debentures.com.br/exploreosnd/consultaadados/sndemumclique/) into an Excel spreadsheet.
After loading this page I have to manually choose an option from the "Código do Ativo" dropdown list, and then choose "Agenda".
Is there a way I can do it automatically via VBA?
For example: selecting "RDVT11" from the "Código do Ativo" dropdown list, selecting "Agenda" and then downloading the data from the table that will appear in the bottom part of the page?
My macro so far:
Private Sub Agenda()
Sheets("Dados").Select
Dim ProductionAddress As String
ProductionAddress = "http://www.debentures.com.br/exploreosnd/consultaadados/sndemumclique/x_pu_historico_r.aspx?"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Silent = True
.Visible = True
.Navigate ProductionAddress
End With
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
ie.document.getElementByid("ctl00_ddlAti").Value = "RDVT11|11001110111100001"
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
Set objButton = ie.document.getElementByid("ctl00_x_agenda_r")
objButton.Focus
objButton.Click
While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
ie.Quit
Set ie = Nothing
End Sub
You need to capture the request that a browser sends when the dropdown is activated. Open up Chrome dev tools and watch the network tab. You will see a POST request to
sndemumclique/
. This will have some headers and form data. Your code will need to basically replicate this request. Likely, not all of the header and form fields are required but there is not way to know without trying.Here are all 3 parts. The making two selections and the writing of the table to the sheet.
Notes:
① Making first selection:
To make the
RDVT11
selection, I first use theId
of the dropdown to capture the element in a variable with:Next, I loop the drop down options, using
a.getElementsByTagName("Option")
to generate the collection which I loop over. When the target selection text is found, I set that option toSelected
and exit the loop.② Making Agenda selection:
I then target the
agenda
option ofSobre e emissão
by itsid
andclick
it and wait for a refresh of the page:③ Getting the table and writing to sheet:
I then target the table that is loaded by its
id
. This is done within a loop to ensure the table is present:I finally, loop the rows and columns in the table, writing out to the
Activesheet
.Code:
Data on page (sample)
Code output (sample):