djburnheim
Technical User
I'm just trying to clean up some code and am trying to workout how to handle erros in a For/Next function. This is what I have at the moment..
***code***
Private Sub cmdStats_Click()
Dim FileName, Available, Logged As String
Available = "J:\Tsdshared\CallCentre\Weekly stats\Tech Team\Available*.csv"
Logged = "J:\Tsdshared\CallCentre\Weekly stats\Tech Team\Logged*.csv"
FileName = Available
For f = 1 To 2
On Error GoTo ErrorHandler
Workbooks.OpenText FileName:=FileName, DataType:=xlDelimited, Comma:=True
Sheets(1).Select
Sheets(1).Move After:=Workbooks("TECH Not Ready Times(v2).xls"
.Sheets(SheetNum)
FileName = Logged
SheetNum = SheetNum + 1
Sheets(1).Select
Next f
Kill Available
Kill Logged
frmWeeklyStats.Show
ErrorHandler:
MsgBox "There are no ACD Stats!", vbCritical + vbOKOnly, "Can Not Open ACD Stats"
End Sub
***End Code***
If the error occurs on the first step then there isn't a problem but if it's on the second step I want to revert the workbook back to the way it was before the start of the procedure..hope this makes sense.
Dave
***code***
Private Sub cmdStats_Click()
Dim FileName, Available, Logged As String
Available = "J:\Tsdshared\CallCentre\Weekly stats\Tech Team\Available*.csv"
Logged = "J:\Tsdshared\CallCentre\Weekly stats\Tech Team\Logged*.csv"
FileName = Available
For f = 1 To 2
On Error GoTo ErrorHandler
Workbooks.OpenText FileName:=FileName, DataType:=xlDelimited, Comma:=True
Sheets(1).Select
Sheets(1).Move After:=Workbooks("TECH Not Ready Times(v2).xls"
FileName = Logged
SheetNum = SheetNum + 1
Sheets(1).Select
Next f
Kill Available
Kill Logged
frmWeeklyStats.Show
ErrorHandler:
MsgBox "There are no ACD Stats!", vbCritical + vbOKOnly, "Can Not Open ACD Stats"
End Sub
***End Code***
If the error occurs on the first step then there isn't a problem but if it's on the second step I want to revert the workbook back to the way it was before the start of the procedure..hope this makes sense.
Dave