Download URL to File - HELP!

scwisely

New Member
Joined
Jun 21, 2019
Messages
22
Hi All - I have the below code, which works fine, however it stops running when it hits an empty cell - I need this to run whether the cell has a url or not...(some of the cells are blank or don't have a URL yet... So I need it to run from D10:D1000 regardless. Can someone help me with the code to fix this? Im new to VBA so if you can create the full code with correction - I'd greatly appreciate it!

Code:
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 Function DownloadURLtoFile(sSourceURL As String, _
sLocalFileName As String) As Boolean
 


DownloadURLtoFile = URLDownloadToFile(0&, _
    sSourceURL, sLocalFileName, &H10, 0&) = 0&


End Function


Sub DownLoadFiles()
Dim cell As Range, rngListOfURL As Range


Const PTH = "C:\BadgeIcons\"   'this is your save to location


Set rngListOfURL = Sheet16.Range("D10:D1000")   'amend as appropriate


For Each cell In rngListOfURL
    If DownloadURLtoFile(cell.Value, PTH & cell.Offset(, 2).Value & ".bmp") Then
       cell.Offset(, 50).Value = "Successfully downloaded"
    Else
        cell.Offset(, 50).Value = "Error - no download"
    End If
Next cell
  
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
When I tested your code, the macro didn't stop when it came across an empty or blank cell. In any case, to skip these cells, try...

Code:
For Each cell In rngListOfURL
    If Len(cell) > 0 Then
        If DownloadURLtoFile(cell.Value, PTH & cell.Offset(, 2).Value & ".bmp") Then
           cell.Offset(, 50).Value = "Successfully downloaded"
        Else
            cell.Offset(, 50).Value = "Error - no download"
        End If
    End If
Next cell

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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