title: Get data from SIDRA (Brazil) with VBA
body: <p>I am trying to automate my worksheet with data from the SIDRA website, a Brazilian database.</p>
  
  <p>Edit: I am trying to download the Industrial Production data. The link provided takes to the series table, than I have to select in "Variável" the first box (Índice de base fixa sem ajuste sazonal (Base: média de 2012 = 100) (Número-índice)), in "Seções e atividades industriais (CNAE 2.0)" also first box, and in "Mês" (month in Portuguese) all the boxes (the whole time series). </p>
  
  <p>I have this code:</p>
  
  <pre><code>Private Sub FazDownload(nomed As String, nomea As String)
  Dim FileNum As Long
  Dim FileData() As Byte
  Dim MyFile As String
  Dim WHTTP As Object
  
  On Error Resume Next
      Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
      If Err.Number &lt;&gt; 0 Then
          Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
      End If
  On Error GoTo 0
  
  MyFile = "http://www.sidra.ibge.gov.br/download/" &amp; nomed &amp; ".tsv"
  
  
  WHTTP.Open "GET", MyFile, False
  WHTTP.Send
  FileData = WHTTP.ResponseBody
  Set WHTTP = Nothing
  
  FileNum = FreeFile
  Open ThisWorkbook.Path &amp; "\" &amp; nomea &amp; ".tsv" For Binary Access Write As #FileNum
      Put #FileNum, 1, FileData
  Close #FileNum
  
  End Sub
  
  Private Sub pim_setoresindustria()
  
  Dim ie As Object
  Dim optCollection
  
  Dim nomeper As String
  nomeper = "industria" &amp; DateDiff("s", #1/1/1970#, Now())
  
  'Starts Internet Explorer
  Set ie = CreateObject("InternetExplorer.Application")
  Application.Wait (Now + TimeValue("0:00:02"))
  
     'Opens IE in the SIDRA webpage
      ie.Navigate "http://www.sidra.ibge.gov.br/bda/tabela/listabl.asp?z=t&amp;o=1&amp;i=P&amp;c=3653"
      ie.Visible = True
  
      While ie.Busy
          DoEvents
      Wend
  
      While ie.Busy
          DoEvents
      Wend
  
      'Selects the characteristics of the table -- HERE IT STOPS WORKING
      Application.Wait (Now + TimeValue("0:00:02"))
          ie.Document.all("opv").Value = 1
          ie.Document.all("pov").Value = 3
          ie.Document.all("orv").Value = 3
  
          'Selects specific data series on the menu 
          Set optCollection = ie.Document.all("sev").Options
              For Each opt In optCollection
                  opt.Selected = (opt.Value = 3135 Or opt.Value = 3134)
                  'Or opt.Value = 3136 Or opt.Value = 3137 Or opt.Value = 3138)
              Next
  
  
          ie.Document.all("opc544").Value = 2
          ie.Document.all("poc544").Value = 3
          ie.Document.all("orc544").Value = 2
  
          ie.Document.all("opp").Value = 2
          ie.Document.all("pop").Value = 2
  
          ie.Document.all("compressao").Click
          ie.Document.all("compressao").Click
  
          ie.Document.all("formato").Value = 4
          ie.Document.all("modalidade").Value = 1
          ie.Document.all("arquivo").Value = nomeper
  
          ie.Document.all("gera").Click
  
      While ie.Busy
          DoEvents
      Wend
  
      'Waits 1 minute to start the download
      'Application.Wait (Now + TimeValue("0:01:00"))
      FazDownload nomeper, "pim"
  
  ie.Quit
  
  End Sub
  </code></pre>
  
  <p>The code is supposed to open the data table from the SIDRA website, and copy it to the worksheet. It used to work (my company has been updating this data for a while), but recently it stoped working. The person that wrote it does not work with us anymore, and I can't understand the part where it selects the options from the SIDRA table. (marked with -- HERE IT STOPS WORKING)</p>
  
  <p>Could anyone help me? </p>