The following code takes the results of previous working code (not shown here) that updates an existing Excel file.
When this code executes it properly calls the Excel Message:
"this file already exists in this location. Do you want to replace it?"
with the Yes | No | Cancel buttons.
If the user selects "Yes" there is no error.
If the user selects "No" or "Cancel" the Run-time error '1004' is thrown, with message "Method 'SaveAs' of Object '_Workbook' failed"
I need some help with an error handler, but don't know the proper way, or location to put it.
I placed an handling routine inside the "With" statement as such:
and it works...but is not optimal.I need to capture the user response "Cancel" to exit sub. otherwise they are in an endless loop.
any ideas would be greatly appreciated.
Code:
With ActiveWorkbook.Sheets("mysheetname")
.Copy
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & Application.PathSeparator & "mysheetname", FileFormat:=51
End With
When this code executes it properly calls the Excel Message:
"this file already exists in this location. Do you want to replace it?"
with the Yes | No | Cancel buttons.
If the user selects "Yes" there is no error.
If the user selects "No" or "Cancel" the Run-time error '1004' is thrown, with message "Method 'SaveAs' of Object '_Workbook' failed"
I need some help with an error handler, but don't know the proper way, or location to put it.
I placed an handling routine inside the "With" statement as such:
Code:
With ActiveWorkbook.Sheets("mysheetname")
On Error GoTo ErrHandle
.Copy
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & Application.PathSeparator & "mysheetname", FileFormat:=51
ErrHandle:
If Err.Number = 1004 Then
MsgBox "File exists, you must select 'Yes" to overwrite"
Resume
End If
End With
and it works...but is not optimal.I need to capture the user response "Cancel" to exit sub. otherwise they are in an endless loop.
any ideas would be greatly appreciated.