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.
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