VBA works on step through but not when run normally?

FatalLordes

Board Regular
Joined
Dec 22, 2017
Messages
78
Office Version
  1. 365
Platform
  1. Windows
Hi all. I have the below code which is set to download the file from the URL and rename the file with the name in C and append the time in HHMMSS at the end as likely there will be more than one file for the name in C and I didn't know how to add on a number like NAME02 if NAME01 exists, etc. I think this is the issue but I could be wrong because the issue is the script works fine when I F8 step through the script, getting the multiple files for the same name, but it only gets one file for the name if I just run the script (because I assume it runs that fast the seconds haven't had a chance to click over). If my logic is right and it is running too quick, could someone perhaps point me as to how I would append a number to the name if the prior number exists (eg., if NAME01 exists, go to NAME02, etc). Or is there some other way to do it? Thanks in advance.

VBA Code:
Option Explicit

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

Dim Ret As Long

'~~> This is where the images will be saved. Change as applicable
Const FolderName As String = "C:\PPF\CatMedical\"

Sub DownloadCatMedical()
    Dim ws As Worksheet
    Dim LastRow As Long, i As Long
    Dim strPath As String

    '~~> Name of the sheet which has the list
    Set ws = Sheets("Medical")

    LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row

    For i = 2 To LastRow '<~~ 2 because row 1 has headers
        strPath = FolderName & ws.Range("C" & i).Value & Format(Time, "hhmmss") & ".jpg"

        Ret = URLDownloadToFile(0, ws.Range("I" & i).Value, strPath, 0, 0)

        If Ret = 0 Then
            ws.Range("Z" & i).Value = "File successfully downloaded"
        Else
            ws.Range("Z" & i).Value = "Unable to download the file"
        End If
    Next i
End Sub
 
Is your problem solved if you save the workbook prior to downloading another copy of a file ?
 
Upvote 0

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