Consulting

Results 1 to 9 of 9

Thread: Download Multiple tables following URLs list in an Excel's worksheet Through VBA

  1. #1
    VBAX Regular
    Joined
    Jun 2021
    Posts
    22
    Location

    Download Multiple tables following URLs list in an Excel's worksheet Through VBA

    Hello, VBA gurus,
    I am a VBA learner. I need an assistance on how to download bulk of tables from a website automatically. I was told an excel VBA can perform the logic. I have the list of all the URLs of the pages which contain the tables. The workbook for the list is attached. From the list, I want a VBA script that will pick the URL one after the other in column B as shown, open, download and save it with the title in column A as shown in a folder on the window ("C:\Users\Akinyele\Desktop\Stock HV").
    I have copied so many scripts online like the one below, but it has not been working. Your help will be greatly appreciated. Thanks.
    Option Explicit
    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
    
    Dim Ret As Long
    
    '~~> This is where the table will be saved. Change as applicable
    Const ParentFolderName As String = "C:\Users\Akinyele\Desktop\Stock HV"
    
    Sub Sample()
        Dim ws As Worksheet
        Dim LastRow As Long, i As Long
        Dim Folderpath, strPath As String
    
        Set ws = Sheets("Sheet1")
    
        LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
    
        For i = 1 To LastRow
      
            Folderpath = ParentFolderName & ws.Range("A" & i).Value & ""
      
            If Len(Dir(Folderpath, vbDirectory)) = 0 Then
                MkDir Folderpath
            End If
      
            strPath = Folderpath & "File" & i & ".csv"
            Ret = URLDownloadToFile(0, ws.Range("B" & i).Value, strPath, 0, 0)
    
            If Ret = 0 Then
                ws.Range("C" & i).Value = "File successfully downloaded"
            Else
                ws.Range("C" & i).Value = "Unable to download the file"
            End If
    
        Next i
     End Sub

    Attached Files Attached Files

  2. #2
    VBAX Regular
    Joined
    Sep 2023
    Posts
    99
    Location
    There is a button on the page that allows you to download a csv file. The URL it uses is:

    https://query1.finance.yahoo.com/v7/finance/download/AMAT?period1=1680858276&period2=1712480676&interval=1d&events=history&inclu deAdjustedClose=true

    You can use the code you have but instead use this URL and replace the /AMAT? with each of the ones you want downloaded, if you're running this in 64-bit Office add PrtSafe to the declaration of the function to download the file.
    Private Declare PtrSafe 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


    This simple code snippet downloaded the first in your list
        Dim downloadURL As String
        downloadURL = "https://query1.finance.yahoo.com/v7/finance/download/AMAT?period1=1680858276&period2=1712480676&interval=1d&events=history&includeAdjustedClose=true"
       Dim ret As Variant
        ret = URLDownloadToFile(0, downloadURL, "F:\temp\test.csv", 0, 0)

  3. #3
    VBAX Regular
    Joined
    Jun 2021
    Posts
    22
    Location
    Quote Originally Posted by jdelano View Post
    There is a button on the page that allows you to download a csv file. The URL it uses is:

    https://query1.finance.yahoo.com/v7/finance/download/AMAT?period1=1680858276&period2=1712480676&interval=1d&events=history&inclu deAdjustedClose=true

    You can use the code you have but instead use this URL and replace the /AMAT? with each of the ones you want downloaded, if you're running this in 64-bit Office add PrtSafe to the declaration of the function to download the file.
    Private Declare PtrSafe 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


    This simple code snippet downloaded the first in your list
        Dim downloadURL As String
        downloadURL = "https://query1.finance.yahoo.com/v7/finance/download/AMAT?period1=1680858276&period2=1712480676&interval=1d&events=history&includeAdjustedClose=true"
    
    
        Dim ret As Variant
        ret = URLDownloadToFile(0, downloadURL, "F:\temp\test.csv", 0, 0)
    
    
    Really appreciate.
    Do you mean I will be altering the code for each stock I am to download?
    I just released the sample for testrun. I have up to 200 of such urls.
    Thanks.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    You can try something like this:
    Private Declare PtrSafe 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
    Sub blah()
    DownloadURLPt1 = "https://query1.finance.yahoo.com/v7/finance/download/"
    DownloadURLPt2 = "?period1=1680858276&period2=1712480676&interval=1d&events=history&includeAdjustedClose=true"
    Dim ret As Variant
    
    For Each cll In Range("A1:A10").Cells
      ret = URLDownloadToFile(0, DownloadURLPt1 & cll.Value & DownloadURLPt2, "C:\Users\Akinyele\Desktop\Stock HV\" & cll.Value & ".csv", 0, 0)
    Next cll
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    How about loading them into PowerQuery and then loading into Excel. You could have a table of URLs in Excel that you use as the source for a base query that then gets the rest.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by Bob Phillips View Post
    How about loading them into PowerQuery and then loading into Excel. You could have a table of URLs in Excel that you use as the source for a base query that then gets the rest.
    Yes, as a starting point, see attached.
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  8. #8
    VBAX Regular
    Joined
    Jun 2021
    Posts
    22
    Location
    I am eternally grateful. It works perfectly. Thanks.

  9. #9
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    akin, did you read and understand the rules about cross posting?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Tags for this Thread

Posting Permissions

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