Import web data in excel using VBA

前端 未结 2 1406
终归单人心
终归单人心 2020-12-04 00:37

I want to import MutualFundsPortfolioValues to Excel. I don\'t know how to import data from a web site which I need to do is import web data to Excel within 2 different date

相关标签:
2条回答
  • 2020-12-04 01:18

    Here is the code to import data using IE Automation.

    Input Parameters (Enter in Sheet1 as per screenshot below)
    start date = B3
    end date = B4
    Şirketler = B5 (It allows multiples values which should appear below B5 and so on)

    enter image description here

    ViewSource of page input fileds enter image description here

    How code works :

    • The code creates object of Internet Explorer and navigates to site
    • Waits till the page is completely loaded and ready. (IE.readystate)
    • Creates the object html class
    • Enter the values for the input fields from Sheet1 (txtDateBegin,txtDateEnd , lstCompany)
    • Clicks on the submit button
    • Iterates thru each row of table dgFunds and dumps into excel Sheet2

    Code:

       Dim IE As Object
    Sub Website()
    
    
        Dim Doc As Object, lastRow As Long, tblTR As Object
        Set IE = CreateObject("internetexplorer.application")
        IE.Visible = True
    
    navigate:
        IE.navigate "http://www.spk.gov.tr/apps/MutualFundsPortfolioValues/FundsInfosFP.aspx?ctype=E&submenuheader=0"
    
        Do While IE.readystate <> 4: DoEvents: Loop
    
        Set Doc = CreateObject("htmlfile")
        Set Doc = IE.document
    
        If Doc Is Nothing Then GoTo navigate
    
        Set txtDtBegin = Doc.getelementbyid("txtDateBegin")
        txtDtBegin.Value = Format(Sheet1.Range("B3").Value, "dd.MM.yyyy")
    
        Set txtDtEnd = Doc.getelementbyid("txtDateEnd")
        txtDtEnd.Value = Format(Sheet1.Range("B4").Value, "dd.MM.yyyy")
    
    
        lastRow = Sheet1.Range("B65000").End(xlUp).row
        If lastRow < 5 Then Exit Sub
    
        For i = 5 To lastRow
    
            Set company = Doc.getelementbyid("lstCompany")
            For x = 0 To company.Options.Length - 1
                If company.Options(x).Text = Sheet1.Range("B" & i) Then
                    company.selectedIndex = x
    
                    Set btnCompanyAdd = Doc.getelementbyid("btnCompanyAdd")
                    btnCompanyAdd.Click
                    Set btnCompanyAdd = Nothing
    
                    wait
                    Exit For
                End If
            Next
        Next
    
    
        wait
    
        Set btnSubmit = Doc.getelementbyid("btnSubmit")
        btnSubmit.Click
    
        wait
    
        Set tbldgFunds = Doc.getelementbyid("dgFunds")
        Set tblTR = tbldgFunds.getelementsbytagname("tr")
    
    
    
        Dim row As Long, col As Long
        row = 1
        col = 1
    
        On Error Resume Next
    
        For Each r In tblTR
    
            If row = 1 Then
                For Each cell In r.getelementsbytagname("th")
                    Sheet2.Cells(row, col) = cell.innerText
                    col = col + 1
                Next
                row = row + 1
                col = 1
            Else
                For Each cell In r.getelementsbytagname("td")
                    Sheet2.Cells(row, col) = cell.innerText
                    col = col + 1
                Next
                row = row + 1
                col = 1
            End If
        Next
    
        IE.Quit
        Set IE = Nothing
    
        MsgBox "Done"
    
    End Sub
    
    Sub wait()
        Application.wait Now + TimeSerial(0, 0, 10)
        Do While IE.readystate <> 4: DoEvents: Loop
    End Sub
    

    Ouput table in Sheet 2

    enter image description here

    HTH

    0 讨论(0)
  • 2020-12-04 01:18

    you could read about it by clicking here then use the macro recorder to record doing it, this will give you some code to work with and if you need more help just ask

    here is another question on Stack Overflow: Importing data from web page with diffrent Dates in excel using VBA code

    basically you're going to add a QueryTable object to the QueryTables collection of the Active worksheet in Excel.

    Here is the MSDN reference on Query Tables: MSDN Library: Excel 2007 Querytables Add method

    0 讨论(0)
提交回复
热议问题