[FONT="]hey guys![/FONT]
[FONT="]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="]i added an errorhandler to deal with this issue, once an error occurs the subroutine closes the file without saving it.[/FONT]
[FONT="]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="]anyone [/FONT][FONT="]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]
[FONT="]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="]i added an errorhandler to deal with this issue, once an error occurs the subroutine closes the file without saving it.[/FONT]
[FONT="]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="]anyone [/FONT][FONT="]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]