errorhandler only works once

orihait

New Member
Joined
Feb 10, 2017
Messages
2
[FONT=&quot]hey guys![/FONT]
[FONT=&quot]tricky question, i have the following code that opens excel files in the folders i specify in the "files" sheet. some of the files are old and dont have certain sheets which i added in later versions.[/FONT]
[FONT=&quot]i added an errorhandler to deal with this issue, once an error occurs the subroutine closes the file without saving it.[/FONT]
[FONT=&quot]apparently this only works once and when it gets the the second file that doesnt have the desired sheet i get a subscript out of range error.[/FONT]
[FONT=&quot]anyone [/FONT][FONT=&quot]knows what i'm doing wrong?

Sub fixit()
On Error GoTo errorhandler
passwrd = InputBox("who is this?", "developer function")
errorcounter = 0
If passwrd= "jareth" Then
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
rikuzname = ActiveWorkbook.Name
lineend = Sheets("files").Range("e2").Value
cellflagind = 10
Set filesystemobj = CreateObject("scripting.filesystemobject")
For lineflag = 1 To lineend
filepath = Workbooks(rikuzname).Worksheets("files").Cells(cellflagind, 6)
Set folderobj = filesystemobj.getfolder(filepath)
For Each fileobj In folderobj.Files
If Strings.InStr(fileobj, "~$") Then
Else
Workbooks.Open Filename:=fileobj, Password:="3028", ReadOnly:=False, writerespassword:="3029", UpdateLinks:=True
projectname = ActiveWorkbook.Name
Workbooks(projectname).Sheets("cashflow").Unprotect Password:="kamil99"
Workbooks(projectname).Worksheets("cashflow").Select
'code starts here
'code ends here
Workbooks(projectname).Sheets("cashflow").Protect Password:="kamil99"
Workbooks(projectname).Worksheets("home").Activate
Workbooks(projectname).Close savechanges:=True
End If
nextfile:
Next fileobj

cellflagind = cellflagind + 1
Next lineflag
MsgBox "done!" & " experienced " & errorcounter & " errors"
Else: MsgBox ("wrong password!")
Workbooks(rikuzname).Save
End If
Exit Sub
errorhandler:
errorcounter = errorcounter + 1
Workbooks(projectname).Close savechanges:=False
GoTo nextfile
End Sub
[/FONT]
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
At the end of your errorhandler block, replace GoTo with Resume.

If you know where your code is tripping up on an error, why not modify the actions within the FOR EACH loop to account for missing sheets?

p.s. Welcome to the forum! Almost forgot my manners!
 
Last edited:
Upvote 0
thanks!, worked like a charm and thanks again for the warm welcome.
i also added err.clear, is it unnecessary?
what do you mean by modifying the actions within the loop?
sorry if i sound like a noob, i am :).
 
Upvote 0
Resume clears the error and is the recommended way to exit an error handling block. Read up on it from an Excel wizard, Chip Pearson: Error Handling In VBA Another good source is Paul Kelly: VBA Error Handling - A Complete Guide - Excel Macro Mastery

On the modifying... I prefer to try to prevent code from ever tripping an error. Using an Error Handler block is sometimes one of the easier ways to deal with a mess, but is more complex for someone to follow your code. For your scenario, if you could identify an error would occur if a certain sheet is missing, an IF statement or a Case Statement would allow you to either run the copy/paste or close the workbook.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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