+ Reply to Thread
Results 1 to 11 of 11

Error catching and repeat macro

Hybrid View

  1. #1
    Registered User
    Join Date
    05-22-2014
    Posts
    16

    Error catching and repeat macro

    Hi All,

    I am running a series of macros including 4 downloads - when I run the series I am getting a run time 1004 error with the download but when I then go to debug there is nothing wrong with the code and it runs normally when I click play. Is there a line of code I can put in to catch the error and repeat the code rather than having to go into vba everytime?

    The download is essential so I do not want to skip over it I just want it to be repeated until it works.


    My code is;


    Sub Download_L2C()
    
    Dim strFileName As String
    Dim strFilePath As String
    
    
    strFilePath = "file"
    strFileName = "L2C.xlsx"
    Workbooks.Open Filename:="" & strFilePath & "" & strFileName & ""
    ActiveWorkbook.SaveCopyAs CreateObject("Wscript.shell").specialfolders("Desktop") & "\Source Data\" & "L2C.xlsx"
    ActiveWorkbook.Close savechanges:=False
    
    Close
    
    
    End Sub

  2. #2
    Valued Forum Contributor Hawkeye16's Avatar
    Join Date
    02-27-2013
    Location
    Holland
    MS-Off Ver
    ├•┤ Pew Pew
    Posts
    441

    Re: Error catching and repeat macro

    You could put an anchor (ie ErrorHandler: ) before the code that is erroring out and put a statement like
    On Error Goto ErrorHandler:
    If you did that I would build in a counter that counts the number of errors and exits the sub if a certain number is reached to avoid a possible infinite loop.

  3. #3
    Registered User
    Join Date
    05-22-2014
    Posts
    16

    Re: Error catching and repeat macro

    Ok....so what would the full new code read like please?

    Unsure

  4. #4
    Valued Forum Contributor Hawkeye16's Avatar
    Join Date
    02-27-2013
    Location
    Holland
    MS-Off Ver
    ├•┤ Pew Pew
    Posts
    441

    Re: Error catching and repeat macro

    Sorry, from your code it seemed like you were more familiar with vba.

    Sub Download_L2C()
    
    Dim strFileName, strFilePath  As String
    Dim errorCount As Integer
    
    errorCount = -1
    On Error GoTo errorHandler:
    
    errorHandler:
    errorCount = errorCount + 1
    if errorCount > 10 then
        msgbox("Error")
        exit sub
    end if
    
    strFilePath = "file"
    strFileName = "L2C.xlsx"
    Workbooks.Open Filename:= strFilePath & strFileName
    ActiveWorkbook.SaveCopyAs CreateObject("Wscript.shell").specialfolders("Desktop") & "\Source Data\" & "L2C.xlsx"
    ActiveWorkbook.Close savechanges:=False
    
    Close
    
    
    End Sub
    This is untested but what I would expect.
    Last edited by Hawkeye16; 06-16-2014 at 05:03 AM.
    Despite the high cost of living, it remains very popular.

    Don't forget to mark threads SOLVED when you get an answer and rep all the geniouses that helped you today!

  5. #5
    Registered User
    Join Date
    05-22-2014
    Posts
    16

    Re: Error catching and repeat macro

    Apologies - I am in some aspects but I am learning as I go along.

    Thanks for this code - going to test during today and i'll come back to you

  6. #6
    Registered User
    Join Date
    05-22-2014
    Posts
    16

    Re: Error catching and repeat macro

    No joy, still throwing up the 1004 error saying the file cannot be found for one of several reasons.

    I have moved this macro to be the very first one and it has worked fine - just having got a clue why the good code is throwing up errors yet working??

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Error catching and repeat macro

    What is the reason for the file not being found?
    If posting code please use code tags, see here.

  8. #8
    Registered User
    Join Date
    05-22-2014
    Posts
    16

    Re: Error catching and repeat macro

    Run-time error '1004': Microsoft Excel cannot access the file 'L2C.xlsx'. There are several possible reasons:
    •The file name or path does not exist
    •The file is being used by another program.

    and then it highlights this line of code - have moved the macro in the list and has worked since 3 times. Any ideas or an anamoly?

    Workbooks.Open Filename:= strFilePath & strFileName

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Error catching and repeat macro

    Where is the file supposed to be located?

  10. #10
    Registered User
    Join Date
    05-22-2014
    Posts
    16

    Re: Error catching and repeat macro

    At a URL so it says the URL cannot be found, yet when I go into the code it works fine - hence the confusion

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Error catching and repeat macro

    Are you just downloading and renaming the file?

    If you are then perhaps you could use the Windows API function URLDownloadFile.

    For example,
    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
    
    Function DownloadFileFromWeb(strURL As String, strSaveName As String, strSavePath As String) As Boolean
    Dim returnValue As Long
    
        returnValue = URLDownloadToFile(0, strURL, strSavePath & strSaveName, 0, 0)
    
        DownloadFileFromWeb = returnValue = 0
    
    End Function
    which you could use like this.
    Dim FileDownloadSuccesful As Boolean
    Dim Response As VbMsgBoxResult
    Dim strFilePath As String
    Dim strFileName As String
    
        Do
            FileDownloadSuccesful = DownloadFileFromWeb(strFilePath & strFileName, strFileName, CreateObject("Wscript.shell").specialfolders("Desktop") & "\Source Data\")
            If Not FileDownloadSuccesful Then
                Response = MsgBox("Download unsuccesful, do you want to try again?", vbYesNo, "Download failed")
    
                If Response = vbNo Then Exit Sub
            End If
        Loop Until FileDownloadSuccesful

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Catching error on Application.GetOpenFilename embed in Open For Input
    By Clevis in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-27-2013, 02:04 PM
  2. Proper error catching runtime error 1004
    By randell.graybill in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-14-2010, 01:07 AM
  3. I keep catching Salmon instead of Macro
    By juvleys in forum Excel General
    Replies: 1
    Last Post: 07-02-2008, 07:48 PM
  4. Catching an error
    By ChrisMattock in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-04-2006, 05:15 AM
  5. [SOLVED] globally catching any error event?
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-04-2005, 03:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1