ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Error Handling works only once (https://www.excelbanter.com/excel-discussion-misc-queries/133677-error-handling-works-only-once.html)

linglc

Error Handling works only once
 
I have these codes below. The Error Handling works only for the first file in
the folder but not the other files. Does anyone know why? All the files in
the folder do not have Sheet ABC. Thanks in advance for your help.

For Each file In Folder.Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open FileName:=file.Path, UpdateLinks:=xlUpdateLinksNever
On Error GoTo ErrHandler
Sheets("ABC").Select
ErrHandler:
ActiveWorkbook.Close SaveChanges:=True
End If
Next file



joel

Error Handling works only once
 
Try handling error like this. If an error occurs when the select command is
executed it will close tthe workbook, otherwise it will go to On Error
statement and return to normal error handling.

For Each file In Folder.Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open FileName:=file.Path, UpdateLinks:=xlUpdateLinksNever
On Error Resume Next

Sheets("ABC").Select
ActiveWorkbook.Close SaveChanges:=True
On Error GoTo 0
End If
Next file

"linglc" wrote:

I have these codes below. The Error Handling works only for the first file in
the folder but not the other files. Does anyone know why? All the files in
the folder do not have Sheet ABC. Thanks in advance for your help.

For Each file In Folder.Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open FileName:=file.Path, UpdateLinks:=xlUpdateLinksNever
On Error GoTo ErrHandler
Sheets("ABC").Select
ErrHandler:
ActiveWorkbook.Close SaveChanges:=True
End If
Next file




All times are GMT +1. The time now is 07:16 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com