How to import data from internet to excel vba

Asked

Viewed 3,446 times

2

I’m trying to download a table from the site: "http://www.scoutscartola.com/mercado", and I already "got". But there is a problem in my code. I want the user to define the team. This is done by selecting a combo box on the site, well, I even change the combo box data, but the table is not updated.

Down with the code so you can see what I’m talking about.

Sub navega_cartola_fc()

   Dim ie As Object
   Dim Nome As String

   Set ie = New InternetExplorer

   Nome = "Botafogo"

   ie.navigate "http://www.scoutscartola.com/mercado"

   ie.Visible = True

   While ie.Busy: Wend

   For Each obj In ie.Document.all.Item("data[filtro_time]")

        If obj.innerText = Nome Then

            obj.Selected = True

        End If

   Next obj

   Dim r, c As Integer
   Dim elemCol As Object

   Set elemCol = ie.Document.getElementsByTagName("tbody")

   Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 3)

   For r = 0 To elemCol(0).Rows.Length - 1

     For c = 0 To elemCol(0).Rows(r).Cells.Length - 1

       ActiveSheet.Cells(r + 1, c + 1) = elemCol(0).Rows(r).Cells(c).innerText

     Next c

   Next r

After correctly activating this combo box, excel should also change another combo box at the bottom of the site (one that says how many players want to filter, because this comes by default 25 and I would like to at least 100, not to risk staying player out of the search)...

Would anyone know what to do in this case? Because all the materials I see, the person changes the combo box as I did, but there is a button on the site itself that "updates" the data. But in this my case has no update button, the update is done so the team is selected in combo box - by the site - and this does not occur when I do by vba.

  • Welcome to the site. Some edits have been made to your question, in order to improve formatting and facilitate reading.

  • Thank you. I even had identação. but when copying paste did not come. another error is in the first one where it says : "If obj.innerText = Link Then" should not = Link and yes = Name only that I have not yet learned to edit

  • I just selected the text and clicked on the code formatting. But you are free to edit the question. Just click "edit". Take a look at the tools that appear at the top of the field where you type the text. They will help you format the content in the best way possible. And Happy 2017 :D.

  • It’s... I’m beginning to understand how the edition works. Thanks for the tip. And happy 2017 for you too.

1 answer

1


Follow the following code:

Sub navega_cartola_fc()

  Dim ieApp As Object
  Dim Nome As String

  Set ieApp = New InternetExplorer

  Nome = "Botafogo"

  ieApp.navigate "http://www.scoutscartola.com/mercado"

  ieApp.Visible = True
  While ieApp.Busy: Wend

  'atualiza a pagina depois de preencher os valores dos campos a procurar
  Do While ieApp.Busy: DoEvents: Loop
  Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop

  'altera o valor da combobox
  Set ieDoc = ieApp.Document
  Set ObjAaa =ieDoc.getElementById("tmercado_length").getElementsByTagName("select")(0)
  Dim evt As Object
  Set evt = ieDoc.createEvent("htmlevents")
  evt.initEvent "change", True, False

  ObjAaa.Value = 100
  'Vai selecionar este valor -> <option value="-1">1000</option>
  'Se quiseres outro valor altera consoante o valor do "value"
  ObjAaa.dispatchEvent evt

  '<<<< new update >>>>
  Set ieDoc1 = ieApp.Document
  Set ObjAaaTime = ieApp.Document.all.Item("data[filtro_time]")(0)
  Dim evt1 As Object
  Set evt1 = ieDoc.createEvent("htmlevents")
  evt1.initEvent "change", True, False

  ObjAaaTime.Value = Nome
  'Vai selecionar este valor -> <option value="botafogo">Botafogo</option>
  'Se quiseres outro valor altera consoante o valor do "value"
  'se vais fornecer este valor pelo excel deves de criar uma espécie de uma lista fixa com todos os valores possíveis
  ObjAaaTime.dispatchEvent evt1

  'volta a atualizar a página
  Do While ieApp.Busy: DoEvents: Loop
  Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop

  Dim r, c As Integer
  Dim elemCol As Object

  Set elemCol = ieApp.Document.getElementsByTagName("tbody")

  Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 3)

  For r = 0 To elemCol(0).Rows.Length - 1
      For c = 0 To elemCol(0).Rows(r).Cells.Length - 1
          ActiveSheet.Cells(r + 1, c + 1) =elemCol(0).Rows(r).Cells(c).innerText
      Next c
  Next r

  ieApp.Quit

  Set elemCol = Nothing
  Set ieApp = Nothing

 End Sub
  • Good afternoon jsantos1991. The code managed to change the combo box for the number of players, but did not "activate" the combo box for the team. Would you know how to do that? Remembering that the team is defined by the variable Name, which in this example is locked in the team Botafogo, but that later I will link to some cell.

  • Under: "Dim evt As Object" appears the error => Duplicate Declaration in the current scope. I switched to Dim evt1 As Object solved. However in: Objaaatime.Value = "Botafogo" gave error => The variable of the block or the variable of the block 'with' was not defined.

  • I just got here. What should I do now? I edit your tip by putting the code that solved or edit my question? Saying that it has already been solved. I am new here and do not know the procedures.

  • Then I will edit yours, putting the final code. Ai is good that you give a look if I made some ignorance regarding vba programming.

  • That’s how it turned out. Thank you so much for helping jsantos1991. I’m 5 days away from cracking my head on this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.