Error Handling Used in Loop to separate Files: Why, How, Help!

AlexB123

Board Regular
Joined
Dec 19, 2014
Messages
207
Rewriting a macro for work ... For some reason, the original author used "On Error Resume Next" and any captured Err.Number as a way to branch files in a directory into successes and fails ... I want to remove it entirely, but I am unsure if this accomplishes anything. Supposedly, this divided files into ones "In Good Order" and "Not In Good Order".

What can cause Workbook.Open events to error out?
Is there a way to test what is going on?
Any recommendations for a different approach?

Eager to add any details, if needed.

Code:
DBFile = UCRTempPath & "Response_Upload.xlsx"
    
TodayFileName = UCRSuccessPath3 & Format(Now(), "MMDDYYHHMM") & "_Response_Upload_Reformat_" & WHOISIT & ".xlsm"
        
    If Dir(UCRResponsePath & "*.xls") <> "" Then
        excelfile = Dir(UCRResponsePath & "*.xls")
    Else: excelfile = Dir(UCRResponsePath & "*.xlsx")
    End If
    
    If excelfile = "" Then
        MsgBox ("There are no files to process.  Please rerun when ready.")
    Exit Sub
    End If
    
    Templatelastrow = 1
    x = 1
    Do While excelfile <> ""
        
        If Dir(UCRResponsePath & "*.xls") <> "" Then
            excelfile = Dir(UCRResponsePath & "*.xls")
        Else: excelfile = Dir(UCRResponsePath & "*.xlsx")
        End If
        
        Success = ""
        
        On Error Resume Next
        Set ucrRspWB = Workbooks.Open(UCRResponsePath & excelfile, Password:="")
            Set ucrSh1 = ucrRspWB.Sheets(1)
        Success = (Err.Number = 0)
        
        If Success = True Then
            
            If ucrSh1.Range("A11") = "CHECK NUMBER" And Len(ucrSh1.Range("J11")) + Len(ucrSh1.Range("K11")) + Len(ucrSh1.Range("L11")) + Len(ucrSh1.Range("M11")) + Len(ucrSh1.Range("N11")) + Len(ucrSh1.Range("O11")) + Len(ucrSh1.Range("P11")) + Len(ucrSh1.Range("Q11")) = 132 Then
                UCRlastrow = ucrSh1.Rows.Range("D65000").End(xlUp).Row
                
                ucrSh1.Range("E12:E" & UCRlastrow).Copy
                tmpSh2.Range("A" & Templatelastrow + 1).PasteSpecial xlPasteValues
                
                ucrSh1.Range("J12:Q" & UCRlastrow).Copy
                tmpSh2.Range("B" & Templatelastrow + 1).PasteSpecial xlPasteValues
                Templatelastrow = tmpSh2.Rows.Range("A650000").End(xlUp).Row
                
                Name UCRResponsePath & ucrRspWB As UCRSuccessPath & DateStamp & ucrRspWB
                
                tmpSh3.Range("A" & x) = ucrRspWB.Name
                x = x + 1
                ucrRspWB.Close False
            
            Else:
                ucrRspWB.Close False
                Name UCRResponsePath & ucrRspWB As UCRFailPath & ucrRspWB
            End If
            
        Else:
            Name UCRResponsePath & excelfile As UCRFailPath & ucrRspWB
            Err.Clear
        End If
    
    Loop
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hello AlexB123,

Change this line ...
Code:
    On Error Resume Next

To this ...
Code:
    ' On Error Resume Next

This will prevent the error from being trapped. The error will display the error dialog telling you the error number and a give a description of the error. Once you what the error is, you should be able to prevent it from happening.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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