How to skip the codes of errorhandler in a for next loop?

ironny90

Board Regular
Joined
Mar 29, 2022
Messages
72
Office Version
  1. 2010
Platform
  1. Windows
Hi everyone! Just ran into this issue and not sure how to get around it. So the macro is looping through all the excel files in a folder, open each one and make changes. However, a few of the files have different tab names, therefore I want to give the user a notification regarding which files are different so that they can change them later.

The problem is the code keeps running after activeworkbook.close and pop up the msgbox "Please check names of the external tabs..." If I put "Next" before the ErrorHandler, it will not go to the next file... Let me know if you know how to solve the issue and many thanks in advance!!

VBA Code:
Set fso = CreateObject("scripting.filesystemobject")
Set ff = fso.getfolder(folderpath)
For Each file In ff.Files
Workbooks.Open file
On Error GoTo ErrorHandler
.
.
.
ActiveWorkbook.Save
ActiveWorkbook.Close ' for good files
ErrorHandler:
MsgBox "Please check names of the external tabs..."
ActiveWorkbook.Close SaveChanges:=False 'for bad files
Next
End If
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You should put your Error Handler code at the bottom/end of your code (it should NEVER be put in the middle of a loop!).
Then, just put an "Exit Sub" line of code just before the error handler.
Then, if your code gets to that point without error, it will exit the sub without running that code.

See here for more details: Excel VBA Error Handling - All You Need to Know!
 
Upvote 0
@Joe4 Thank you so much for your quick response!! What if I want the codes to continue running after the error msg? Like the user notes which file is different and the codes continues to the next file? Is it doable?
 
Upvote 0
@Joe4 Thank you so much for your quick response!! What if I want the codes to continue running after the error msg? Like the user notes which file is different and the codes continues to the next file? Is it doable?
If you want it to return to the code, just include the line "Resume Next" in your error handling code to return to the code.
You can also use "Err.Clear" to clear any error message before resuming, if you like.
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top