Downloading code bogging excel


Well-known Member
Oct 4, 2018
I have some code that downloads pdf sheets at hte push of a button. The code works like a champ but (I presume because my internet connection is beyond super slow (satellite, 0.34mbps download....), excel goes into a "not responding" mode until the download completes. Is there any way to fix this? Code is attached

Sub Downloady()Dim URL As String
Dim name As String
Dim tstamp As String
Dim Folder0 As String
Dim Folder1 As String
Dim Folder2 As String
Dim folder3 As String
Dim namer As String
Dim Date0 As String
Dim Date1 As String
Dim Date2 As String
Dim Date3 As String
Dim Divider As String
Dim LocalFilePath As String
Dim OldFinalName As String
Dim TempFolderOLD As String
Dim TempFileNEW As String
Dim DownloadStatus As Long
Dim LastRow As Long
Dim Finalname As String
Dim btn As Shape
Dim MyFSO As Object
'Set MyFSO = New Scripting.FileSystemObject
Set MyFSO = CreateObject("Scripting.FileSystemObject")
Dim RW As Long

'Error Checking
'On Error GoTo Err
    ' find last row of data in column B on 'Background'
    LastRow = Sheets("Background").Range("B" & Rows.Count).End(xlUp).Row

    ' loop through rows on 'Background'
    RW = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row
        With Sheets("Background")
            namer = .Range("B" & RW)    'Pub name
            URL = .Range("I" & RW)      'URL to download
            Date0 = .Range("C" & RW)    'Week #
            Date1 = .Range("E" & RW)    'Year #
            Divider = .Range("D" & RW)  '\
            Date2 = .Range("G2")        'base week
            Date3 = .Range("I2")        'base year
        End With
        With Sheets("Setup")
            Folder0 = .Range("B5")    'temp folder (desktop)
            Folder1 = .Range("B7")    'permanent folder (desktop)
            Folder2 = .Range("D7")    'permanent folder
            folder3 = .Range("D5")    'temp Folder
            name = .Range("A1")       'company name
        End With
        TempFolderOLD = Environ("Userprofile") & "\" & Folder0 & "\" & folder3
        tstamp = Format(Now, "mm-dd-yyyy")
        TempFileNEW = TempFolderOLD & "\" & namer & ".pdf"
        LocalFilePath = Environ("Userprofile") & "\" & Folder1 & "\" & Folder2
        OldFinalName = LocalFilePath & Finalname
        Finalname = namer & ".pdf"
        'If these criteria are met, let's begin the download tree
        If Date0 <> Date2 Or Date1 <> Date3 Then
            'Let's assign everything to the temp folder
            'Begin by clearing any possible undeleted/corrupted files from my "temp" folder
            If MyFSO.FolderExists(TempFolderOLD) Then MyFSO.DeleteFolder (TempFolderOLD)
            'Make a new temp folder
            If Not MyFSO.FolderExists(TempFolderOLD) Then MkDir (TempFolderOLD)
            'Attempt download to the temp folder
            DownloadStatus = URLDownloadToFile(0, URL, TempFileNEW, 0, 0)
            'Check for proper download
            If DownloadStatus = 0 Then
                'Delete the old files
                If MyFSO.FileExists(OldFinalName) Then
                    MyFSO.DeleteFile (OldFinalName)
                    MyFSO.CreateFolder (LocalFilePath)
                End If
                'Save temp files to replace old files
                'TempFileNEW.SaveAs Filename:=LocalFilePath, FileFormat:=xlTypePDF
                If MyFSO.FileExists(OldFinalName) Then MyFSO.DeleteFile (OldFinalName)
                MyFSO.CopyFile Source:=TempFileNEW, Destination:=LocalFilePath & "\"
                'Now delete temp files
                If MyFSO.FolderExists(TempFolderOLD) Then MyFSO.DeleteFolder (TempFolderOLD)
                'Now update excel sheet to show download passed
                MsgBox "File Downloaded. Check in this path: " & LocalFilePath
                With Sheets("Background")
                    .Range("F" & RW) = tstamp
                    .Range("G" & RW) = "SAT"
                    .Range("C" & RW) = Format(Now, "ww", vbWednesday)
                    .Range("E" & RW) = Format(Now, "yy")
                    .Range("D" & RW) = "/"
                    'date formating
                    .Range("C" & RW).HorizontalAlignment = xlRight
                    .Range("D" & RW).HorizontalAlignment = xlGeneral
                    .Range("E" & RW).HorizontalAlignment = xlLeft
                End With
                'If download failed, update excel to show- old files should NOT have been deleted yet but the temp file should be deleted
                MsgBox "Download File Process Failed"
                Sheets("Background").Range("G" & RW) = "FAIL"
                If MyFSO.FileExists(TempFolderOLD) Then
                MyFSO.DeleteFile (TempFolderOLD)
                End If
            End If
            'If the original criteria were met and the download was not necessary, say so

            MsgBox "The most up to date " & namer & " has been downloaded", vbOKOnly, name
        End If
'Error checking
'Exit Sub
'Err: MsgBox (RW)

End Sub

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...