John Carney
asked on
Managing overlapping error handlers
I have some overlapping error handlers and I can't figure out how to make the second one work. The first error handler deals with which of two naming conventions a given file might have. But then I need something that goes to the next cel in [Customers] if neither of the naming conventions are present for the current cel.
How would I write that? Humor me along with my GoTo names. Just a little comic relief in a long, glorious day of coding.
Thanks,
John
How would I write that? Humor me along with my GoTo names. Just a little comic relief in a long, glorious day of coding.
Thanks,
John
Sub OpenBooks()
Dim cel As Range
For Each cel In ActiveSheet.[Customers]
Dim wb As String, wb2 As String
wb = cel & " " & ActiveSheet.[annum] & " " & "Week " & [F1] & ".xls"
wb2 = cel & " " & ActiveSheet.[annum] & " " & "Week " & [F1] & "_2BCoded.xls" '& [XLVersion]
On Error GoTo Paris
ChDir ThisWorkbook.Path & "\..\" & cel & "\3_Working Files\Coded\"
Workbooks.Open Filename:=wb
GoTo Rome
Paris:
Err.Clear
ChDir ThisWorkbook.Path & "\..\" & cel & "\3_Working Files\2BCoded\"
Application.DisplayAlerts = False
On Error GoTo Madrid
Workbooks.Open Filename:=wb2
Rome:
Call AddNewWeek
Madrid:
Err.Clear
Next cel
Application.DisplayAlerts = True
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Perhaps you could use Dir to check if the file(s) exist.
Something like this perhaps, sorry if I go some of the logic wrong:
Something like this perhaps, sorry if I go some of the logic wrong:
Sub OpenBooks()
Dim wbOpen As Workbook
Dim cel As Range
Dim wb As String, wb2 As String
Dim strPath As String
strPath = ThisWorkbook.Path & "\..\" & cel & "\3_Working Files\"
For Each cel In ActiveSheet.[Customers]
wb = cel & " " & ActiveSheet.[annum] & " " & "Week " & [F1] & ".xls"
wb2 = cel & " " & ActiveSheet.[annum] & " " & "Week " & [F1] & "_2BCoded.xls" '& [XLVersion]
If Dir(strPath & "\Coded\" & wb) <> "" Then
Set wbOpen = Workbooks.Open(strPath & "\Coded\" & wb)
End If
If wbOpen Is Nothing Then
If Dir(strPath & "\2BCoded\" & wb2) <> "" Then
Set wbOpen = Workbooks.Open(strPath & "\2BCoded\" & wb2)
End If
End If
If Not wbOpen Is Nothing Then
Call AddNewWeek
End If
Next cel
End Sub
ASKER
Great suggestions, Dave, thanks. Inmorie, I don't think your code will help me in this particular situation, but thank you for posting.
Patrick, there have been many days when that's exactly what I've wanted to say :-)
Thanks,
John
Patrick, there have been many days when that's exactly what I've wanted to say :-)
Thanks,
John
Open in new window
I've never worked up the nerve to include that in production code, though :)