1

estoy intentando traer una tabla de la siguiente página a Excel: https://bscscan.com/address/0x589ae3638468e122DcED9c836e03be82b97b3bcA

para ello estoy utilizando el siguiente código en VBA:

Sub TransaccionesBscscan()
    Dim ieObj As InternetExplorer
    Dim htmlEle As IHTMLElement
    Dim i As Integer
    
    i = 1
    
    Set ieObj = New InternetExplorer
    ieObj.Visible = True
    ieObj.navigate "https://bscscan.com/address/0x589ae3638468e122dced9c836e03be82b97b3bca"
 
    Application.Wait Now + TimeValue("00:00:05")
    
    For Each htmlEle In ieObj.Document.getElementsByClassName("table")(0).getElementsByTagName("tr")

        With ActiveSheet
            .Range("A" & i).Value = htmlEle.Children(0).textContent
            .Range("B" & i).Value = htmlEle.Children(1).textContent
            .Range("C" & i).Value = htmlEle.Children(2).textContent
            .Range("D" & i).Value = htmlEle.Children(3).textContent
            .Range("E" & i).Value = htmlEle.Children(4).textContent
            .Range("F" & i).Value = htmlEle.Children(5).textContent
            .Range("G" & i).Value = htmlEle.Children(6).textContent
            .Range("H" & i).Value = htmlEle.Children(7).textContent
            .Range("I" & i).Value = htmlEle.Children(8).textContent
            
        End With
        
        i = i + 1
    Next htmlEle    
End Sub

El inconveniente es que hay un problema cuando me trae los encabezados porque trae varios nombres de columna en algunas celdas y los agrupa juntos. y además no trae la última columna. Dejo imágenes para que se entienda.

Tabla a traer con 8 columnas.

introducir la descripción de la imagen aquí

Esto lo que pude obtener hasta el momento. Tiene mal los encabezados y no se como traer la última columna...

introducir la descripción de la imagen aquí

Si alguien me da una mano le voy a estar muy agradecido!!

1 respuesta 1

0

Se me ocurren dos opciones:

  1. Utilizas la función Replace para reemplazar las palabras: from, to, filter y clear en la primera fila.
  2. Te saltas la primera fila (cabecera) y escribes a mano los títulos tú mismo.

Aquí te dejo un ejemplo del segundo caso:

Sub TransaccionesBscscan()
    Dim ieObj As InternetExplorer
    Dim htmlEle As IHTMLElement
    Dim i As Integer
    
    i = 1
    
    Set ieObj = New InternetExplorer
    ieObj.Visible = True
    ieObj.navigate "https://bscscan.com/address/0x589ae3638468e122dced9c836e03be82b97b3bca"
 
    Application.Wait Now + TimeValue("00:00:02")
    
    For Each htmlEle In ieObj.Document.getElementsByClassName("table")(0).getElementsByTagName("tr")

        If i = 1 Then
        
            
            With ActiveSheet
                .Range("A" & i).Value = ""
                .Range("B" & i).Value = "Txn Hash"
                .Range("C" & i).Value = "Block"
                .Range("D" & i).Value = "Date"
                .Range("E" & i).Value = "Age"
                .Range("F" & i).Value = "From"
                .Range("G" & i).Value = ""
                .Range("H" & i).Value = "To"
                .Range("I" & i).Value = "Value"
                .Range("J" & i).Value = "Txn Fee"
            End With
            i = i + 1
        
        Else
        
            With ActiveSheet
                .Range("A" & i).Value = htmlEle.Children(0).textContent
                .Range("B" & i).Value = htmlEle.Children(1).textContent
                .Range("C" & i).Value = htmlEle.Children(2).textContent
                .Range("D" & i).Value = htmlEle.Children(3).textContent
                .Range("E" & i).Value = htmlEle.Children(4).textContent
                .Range("F" & i).Value = htmlEle.Children(5).textContent
                .Range("G" & i).Value = htmlEle.Children(6).textContent
                .Range("H" & i).Value = htmlEle.Children(7).textContent
                .Range("I" & i).Value = htmlEle.Children(8).textContent
                .Range("J" & i).Value = htmlEle.Children(9).textContent
                
            End With
            
            i = i + 1
            
        
        End If

    Next htmlEle
End Sub

Así te saltas el primer elemento (htmlEle) y escribes a mano los titulos. Que, por cierto, te faltaba por añadir una columna más.

Un saludo.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.