+ Reply to Thread
Results 1 to 6 of 6

Importing Web data into Microsoft Excel

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Importing Web data into Microsoft Excel

    Why is it possible to import a table from a web page into Excel but not possible to import data from a downloadable (Excel) file into Excel?
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Importing Web data into Microsoft Excel

    Because a table from a website is text so is directly parse-able, and Excel workbook is a binary object that isn't.

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Importing Web data into Microsoft Excel

    Quote Originally Posted by Kyle123 View Post
    Because a table from a website is text so is directly parse-able, and Excel workbook is a binary object that isn't.
    Ah, that explains why then.

    From a practical POV, I will need to manually download the Excel file from the website and then direct the VBA code to the downloaded file to bring it in, right?

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Importing Web data into Microsoft Excel

    Well you could automate the download from VBA read the data and kill the workbook if you so wished. Downloading from the web is pretty trivial, but it gets a little more complex when usernames and passwords are involved

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Importing Web data into Microsoft Excel

    Thanks Kyle. I'll give it a go


    Here's where I have reached so far...

    Option Explicit
    
    Private Const strURL = "http://www.rba.gov.au/statistics/hist-exchange-rates/2010-2013.xls"
    
    Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
      "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal _
        szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
    
    Public Sub DownloadExcel_Read_Kill()
        Dim strDownloadFullPath As String
        Dim wbExcelDownload As Workbook
        Dim ext As String
        Dim buf As Variant
        Dim ret As Long
        Dim lngFileName As Long
    
    '   determine file extension
        buf = Split(strURL, ".")
        ext = "." & buf(UBound(buf))
        buf = vbNull
    
    '   set download file path & name
        lngFileName = Now()
        strDownloadFullPath = Environ("Temp") & "\" & lngFileName & ext
    
    '   download file
        ret = URLDownloadToFile(0, strURL, strDownloadFullPath, 0, 0)
    
    '   check if file downloaded successfully
        If Not ret = 0 Then
            MsgBox "Download failed", vbCritical, "ERROR"
            Exit Sub
        End If
    
    '   file open & read
        Application.ScreenUpdating = False
        Set wbExcelDownload = Workbooks.Open(strDownloadFullPath)
    
        Debug.Print "To insert read code here"
    
    '   file close & kill
        With wbExcelDownload
            .Saved = True
            .ChangeFileAccess Mode:=xlReadOnly
            Kill .FullName
            .Close SaveChanges:=False
        End With
    
        Application.ScreenUpdating = True
    End Sub

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Importing Web data into Microsoft Excel

    Solved. The function to build the array could be more efficient/neater code but it seems to do the job.

    Option Explicit
    Option Private Module
    
    'alter URL & workbook name to suit your needs
    Private Const strURL As String = "http://www.rba.gov.au/statistics/hist-exchange-rates/2010-2013.xls"
    
    #If VBA7 And Win64 Then
        Private Declare PtrSafe Function URLDownloadToFile _
            Lib "urlmon.dll" Alias "URLDownloadToFileA" ( _
                ByRef pCaller As LongPtr, _
                ByVal szURL As String, _
                ByVal szFileName As String, _
                ByVal dwReserve As Long, _
                ByRef lpfnCB As LongPtr) _
            As LongPtr
    #Else
        Private Declare Function URLDownloadToFile _
            Lib "urlmon" Alias "URLDownloadToFileA" ( _
                ByVal pCaller As Long, _
                ByVal szURL As String, _
                ByVal szFileName As String, _
                ByVal dwReserved As Long, _
                ByVal lpfnCB As Long) _
            As Long
    #End If
    
    Public Function DownloadExcel_Read_Kill() As Variant
    'call this function to obtain a customised array from a downloadable workbook
        Dim strDownloadFullPath     As String
        Dim wbExcelDownload         As Workbook
        Dim lngFileName             As Long
    
        Dim ext                     As String
        Dim buf                     As Variant
        Dim ret                     As Long
    
    '   determine file extension
        buf = Split(strURL, ".")
        ext = "." & buf(UBound(buf))
        buf = vbNull
    
    '   set download file path & name
        lngFileName = Now()
        strDownloadFullPath = Environ("Temp") & Application.PathSeparator & lngFileName & ext
        lngFileName = vbNull
    
    '   download file
        ret = URLDownloadToFile(0, strURL, strDownloadFullPath, 0, 0)
    
    '   check if file downloaded successfully
        If Not ret = 0 Then
            MsgBox "Download failed", vbCritical, "ERROR"
            Exit Function
        End If
    
    '   file open & read
        Application.ScreenUpdating = False
        Set wbExcelDownload = Workbooks.Open(FileName:=strDownloadFullPath, ReadOnly:=True, AddtoMRU:=False)
        strDownloadFullPath = vbNullString
    
    '   alter the array function to suit to your own needs
        DownloadExcel_Read_Kill = RbaArrayRange(wbExcelDownload)
    
    '   file close & kill
        With wbExcelDownload
            .Saved = True
            On Error Resume Next
            .ChangeFileAccess Mode:=xlReadOnly
            On Error GoTo 0
            Kill .FullName
            .Close SaveChanges:=False
        End With
    
        Set wbExcelDownload = Nothing
        Application.ScreenUpdating = True
    End Function
    
    Private Function RbaArrayRange(wbWorkbook As Workbook) As Variant
        Dim lngBottom   As Long
        Dim lngTop      As Long
    
        With wbWorkbook
            With .ActiveSheet
                'get last row
                lngBottom = .Cells(Rows.Count, "A").End(xlUp).Row
                'get top row
                lngTop = lngBottom
                Do While .Cells(lngTop - 1, "A").Value = .Cells(lngTop, "A").Value - 1
                    lngTop = lngTop - 1
                Loop
                'set array
                RbaArrayRange = Range(.Cells(lngTop, 1), .Cells(lngBottom, 8))
            End With
        End With
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1