Gerald Higgins
Well-known Member
- Joined
- Mar 26, 2007
- Messages
- 9,258
Thanks in advance for any help !
So I've got a master file, and am writing some code to open individual files one at a time for each of a number of other company locations, suck data from them into the master file, do some other changes to the individual location files, close each of them down and move on to the next location.
This basically works fine.
I now want to put some code in that handles errors, such as what to do if one of the individual location files can't be found.
I want to give the user the option to either bypass that one location and move on to the next location, OR terminate the whole process.
I'm showing a message box in the event that there is a problem opening the file, to give that option to the user.
For some reason this works fine for the first location, but not the second location.
Am I missing something ?
The code is essentially like this - the section in red bold is where the error appears.
Both the files for LOCATION1 and LOCATION2 do NOT exist when I run this test.
For location 1, no error message is shown, except for my user dialog box which correctly gives the option to bypass location 1, or terminate the process.
I then choose the option YES to bypass and move on to location 2.
HOWEVER, for location 2, it seems to ignore the Application.DisplayAlerts = False instruction, doesn't show the dialog box for option 2, and instead gives me a "Run-time error 1004" message saying the file for location 2 cannot be accessed.
I've checked what the status of the Application.DisplayAlerts setting is, and it appears to correctly be FALSE just before it errors out.
Am I missing something ?
How do I get it to suppress the "Run-time error 1004" message for location 2, and instead give me the dialog box for location 2, the same as it does correctly for location 1 ?
Thanks again in advance for any help !
So I've got a master file, and am writing some code to open individual files one at a time for each of a number of other company locations, suck data from them into the master file, do some other changes to the individual location files, close each of them down and move on to the next location.
This basically works fine.
I now want to put some code in that handles errors, such as what to do if one of the individual location files can't be found.
I want to give the user the option to either bypass that one location and move on to the next location, OR terminate the whole process.
I'm showing a message box in the event that there is a problem opening the file, to give that option to the user.
For some reason this works fine for the first location, but not the second location.
Am I missing something ?
The code is essentially like this - the section in red bold is where the error appears.
Rich (BB code):
'***** LOCATION 1 *****
On Error GoTo Location1BypassCheck
GoTo Location1DataPull
Location1BypassCheck:
YesNo = MsgBox("Unable to open LOCATION1 resource file. Do you want to bypass LOCATION1 and continue with other locations ?", vbYesNo)
If YesNo = vbYes Then
DepotFails = "LOCATION1 "
GoTo LOCATION2
Else
GoTo InvalidWeekNo
End If
Location1DataPull:
Application.DisplayAlerts = False
Workbooks.Open(Filename:= _
'***** omitted full directory path for the purposes of this post *****
LOCATION1 resource template.xlsm" _
).RunAutoMacros Which:=xlAutoOpen
ActiveSheet.Unprotect "PASSWORD"
Application.DisplayAlerts = True
'***** omitted code for doing stuff in the LOCATION1 file, for clarity in this post *****
GoTo LOCATION2
LOCATION2:
On Error GoTo Location2BypassCheck
GoTo Location2DataPull
Location2BypassCheck:
YesNo = MsgBox("Unable to open LOCATION2 resource file. Do you want to bypass LOCATION2 and continue with other locations ?", vbYesNo)
If YesNo = vbYes Then
DepotFails = DepotFails & "LOCATION2 "
GoTo LOCATION3
Else
GoTo InvalidWeekNo
End If
LOCATION3DataPull:
Application.DisplayAlerts = False
Workbooks.Open(Filename:= _
'***** omitted full directory path for the purposes of this post *****
LOCATION2 resource template.xlsm" _
).RunAutoMacros Which:=xlAutoOpen
ActiveSheet.Unprotect "PASSWORD"
Application.DisplayAlerts = True
Both the files for LOCATION1 and LOCATION2 do NOT exist when I run this test.
For location 1, no error message is shown, except for my user dialog box which correctly gives the option to bypass location 1, or terminate the process.
I then choose the option YES to bypass and move on to location 2.
HOWEVER, for location 2, it seems to ignore the Application.DisplayAlerts = False instruction, doesn't show the dialog box for option 2, and instead gives me a "Run-time error 1004" message saying the file for location 2 cannot be accessed.
I've checked what the status of the Application.DisplayAlerts setting is, and it appears to correctly be FALSE just before it errors out.
Am I missing something ?
How do I get it to suppress the "Run-time error 1004" message for location 2, and instead give me the dialog box for location 2, the same as it does correctly for location 1 ?
Thanks again in advance for any help !
Last edited: