Stepheny2k2
New Member
- Joined
- Nov 23, 2009
- Messages
- 13
Morning guys,
I have written a VBA procedure which loops through excel files in an array: in turn it opens the file, refreshes the data, saves the file and closes. I would like to write an error handler which stores the file which errored (copied from the loop iteration) and the error description to an array. The error handler should then close the file and continue with the next iteration - e.g.
*Although a generic if error may be better.
My idea is that all files which errored in some way will be stored in an array which I can automatically print/send to a user so they can manually figure out the problem. (This element falls outside the range of this query)
Does anyone have an idea how i can do this?
Below is a simple example of my efforts which only contains one file in the array to make things simpler: the error handler section won't work, it's just there to show my thinking
Using Excel 2010
I have written a VBA procedure which loops through excel files in an array: in turn it opens the file, refreshes the data, saves the file and closes. I would like to write an error handler which stores the file which errored (copied from the loop iteration) and the error description to an array. The error handler should then close the file and continue with the next iteration - e.g.
Code:
If ActiveWorkbook.ReadOnly Then
ActiveWorkbook.Close
Next i
*Although a generic if error may be better.
My idea is that all files which errored in some way will be stored in an array which I can automatically print/send to a user so they can manually figure out the problem. (This element falls outside the range of this query)
Does anyone have an idea how i can do this?
Below is a simple example of my efforts which only contains one file in the array to make things simpler: the error handler section won't work, it's just there to show my thinking
Code:
Sub Refresh_CRIS()
On Error GoTo Errorhandler
Dim routepath As String
routepath = "[URL="file://\\nch\dfs\SharedArea\Private\Test"]\\nch\dfs\SharedArea\Private\Test[/URL]"
ChDir routepath
'
Dim CRISPiv As Variant 'Includes Dailies
Dim Failed As Variant
Dim i
CRISPiv = Array("Waiting-List-Diagnostic.xls")
For Each i In CRISPiv
Workbooks.Open Filename:=routepath & i
ActiveWorkbook.RefreshAll
Application.CalculateFull
Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True
ActiveWorkbook.Close
Next i
Exit Sub
Errorhandler:
On Error Resume Next
Failed = Array(i)
If ActiveWorkbook.ReadOnly Then 'Just an example for testing
ActiveWorkbook.Close
Else
MsgBox "Pivots which failed to refresh:" & Failed '& ", ", 0, "Debug" 'Test with msgbox
End If
End Sub
Using Excel 2010