top of page

Macro de Excel para obtener divisas desde una página bancaria

Código ejemplo de una macro en Excel para obtener tipo de cambio de moneda, obteniendo los datos desde una página web de un banco.

'

'References
'Microsoft HTML Object LIBRARY
'Microsoft Internet Controls

Sub DoBrowse1()
    Dim ie As New SHDocVw.InternetExplorer
    Dim HTMLDoc As MSHTML.HTMLDocument
    Dim HTMLDiv As MSHTML.HTMLDivElement
    Dim pElements As MSHTML.IHTMLElementCollection
    
    'Set ie = CreateObject("Internetexplorer.Application")
    ie.Visible = False
    ie.Navigate "https://www.banamex.com/economia-finanzas/es/mercado-de-divisas/index.html"
    
    ' Wait till IE is fully loaded.
    Do While ie.ReadyState <> READYSTATE_COMPLETE
        DoEvents
    Loop
    
    If Application.Wait(Now + TimeValue("0:00:03")) Then
         ''MsgBox "Time expired"
    
        Set HTMLDoc = ie.document
        Set HTMLDiv = HTMLDoc.getElementById("dat_divisas")
        Set pElements = HTMLDiv.getElementsByTagName("p")
        
        Dim i As Integer
        i = 1
          
        For Each p In pElements
            If i < 4 Then
                Cells(i, "B") = p.innerText
            ElseIf i < 7 Then
                Cells(i - 3, "C") = p.innerText
            ElseIf i < 10 Then
                Cells(i - 6, "D") = p.innerText
              ElseIf i < 13 Then
                Cells(i - 9, "E") = p.innerText
            End If
            
            i = i + 1
            If i = 13 Then i = pElements.Length
            
        Next p
    
    End If
    
    Cells(2, "A") = "Compra"
    Cells(3, "A") = "Venta"
    
    ' Clean up.
    ie.Quit
    Set ie = Nothing
End Sub

 

bottom of page