Download all excel files from a webpage

pkinif

New Member
Joined
Oct 19, 2017
Messages
3
Hi everyone,

Here is my problem :

- I want to dowload all excel files from this site : http://www.stockpup.com/companies/HAS/ and record them on my computer.

I have searched on the internet to find some existing vba code to help me but all includes API function and I do not have enough vba skills to understand those vba code.

Could someone help me?

Thank you in advance,

Pierrick
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Re: Dowload all excel files from a webpage

Wow, thank you so much Haluk! Your vba code is really great. I could dowload all files in 5 minutes
 
Upvote 0
Re: Dowload all excel files from a webpage

You are welcome :)

Hello Haluk,

Wow! You seem to be an VBA god!

I had a query to ask of you. There is a webpage from where I need to extract all the data in the excels and consolidate them in one excel file. The link is: https://www.rbi.org.in/Scripts/WSSViewDetail.aspx?TYPE=Section&PARAM1=2#

When you open the link, the first file is that of 24th November. I am required to consolidate the data in columns B, C and D in different tabs and I need to do this for as far back (essentially all the excel files on the webpage).

I do not have much programming knowledge and I was hoping for your help.

Regards,
Anuj
 
Upvote 0
Re: Dowload all excel files from a webpage

And I am really sorry if I have not adhered to the standards of this forum in any manner. Really tensed due to this assignment..:(
 
Upvote 0
Re: Dowload all excel files from a webpage

When you open the link, the first file is that of 24th November. I am required to consolidate the data in columns B, C and D in different tabs and I need to do this for as far back (essentially all the excel files on the webpage).

Hello,

Actually it seems that; the web site you are referring doesn't like to recieve autamated commands for downloading.

The code below; will list the Excel files on the page and will download the first 5 files. But, the files are corrupted and cannot be opened.

You can try the following code;

Code:
Sub GetFiles()
    '
    '27/11/2017 - Haluk ®
    '
    Dim IE As Object, URL As String
    Dim HTML_Body As Object, HTML_Tables As Object, MyTable As Object
    Dim RetVal As Variant
    Dim FileNum As Long
    Dim FileData() As Byte
    Dim MyFile As String
    Dim WHTTP As Object, WshShell As Object
    Dim strMyDocuments As String
    Dim i As Integer, j As Integer
    
    URL = "https://www.rbi.org.in/Scripts/WSSViewDetail.aspx?TYPE=Section&PARAM1=2#"
    
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Navigate URL
        Do Until IE.ReadyState = 4: DoEvents: Loop
        Do While IE.Busy: DoEvents: Loop
        
        Set HTML_Body = IE.document.Body
        Set HTML_Tables = HTML_Body.GetElementsByTagName("Table")
        Set MyTable = HTML_Tables(1)
        
        For i = 1 To 1374 Step 2
        j = j + 1
        RetVal = MyTable.Rows(i).Cells(1).InnerHTML
        x2 = InStr(1, RetVal, "target")
        Range("A" & j) = Mid(RetVal, 51, x2 - 51 - 2)
        Range("B" & j) = MyTable.Rows(i - 1).Cells(0).InnerText
        Next
    End With
    
    On Error Resume Next
        Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5")
        If Err.Number <> 0 Then
            Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
        End If
    On Error GoTo 0
    
    Set WshShell = CreateObject("WScript.Shell")
    
    strMyDocuments = WshShell.SpecialFolders("MyDocuments")
    MyFolder = strMyDocuments & "\Foreign_Exchange_Reserves"
    
    If Dir(MyFolder, vbDirectory) = Empty Then MkDir MyFolder
    
    For i = 1 To 5
    
        DoEvents
        
        Cells(i, 4).Select
        
        On Error GoTo ErrHandler:
        MyFile = "http://rbidocs.rbi.org.in/rdocs/Wss/DOCs/" & Cells(i, 1)
        
        WHTTP.Open "GET", MyFile, False
        WHTTP.Send
        FileData = WHTTP.ResponseBody

        FileNum = FreeFile
        
        Open MyFolder & "\" & Cells(i, 1) For Binary Access Write As #FileNum
            Put #FileNum, , FileData
        Close #FileNum
        
        Cells(i, 3) = "Download completed"
        
        GoTo NextItem:
        
ErrHandler:
        Cells(i, 3) = "Download NOT completed"
        
NextItem:
    
    Next
    
    MsgBox "Open the folder " & MyFolder & " to view the downladed files ..."
    
    Set WHTTP = Nothing
    
    IE.Quit
    Set HTML_Body = Nothing
    Set HTML_Tables = Nothing
    Set MyTable = Nothing
    Set HTML_TableRows = Nothing
    Set HTML_TableDivisions = Nothing
    Set IE = Nothing
    
End Sub
 
Upvote 0
Re: Dowload all excel files from a webpage

Thank you so very much.

While awaiting your reply, I have manually downloaded these files - 550 in total! The complication that you pointed out makes my approach the right one, however now I am faced with another challenge of getting data out of all these files, which do not have the same structure.

Any thoughts? Thanks again. You are a rockstar!
 
Upvote 0

Forum statistics

Threads
1,215,374
Messages
6,124,569
Members
449,173
Latest member
Kon123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top