“Error handler process” in a “for loop”
I want to use “error handler process” in a “for loop” such that if the condition defined for the loop is not met, the macro increases i(the variable defined for loop) by 1 unit and get back to beginning of the loop .
Consider following macro, if it get “run time error” at i=4, I expect that it continue at i=5.
As the Excel attachment shows, this code dose not work as I expect and stop as i=4 with “run time” error.
Can you give me some guides on this issue?
Option Explicit
Sub forlloop()
Dim cell0 As Range
Dim i As Integer
Dim n As Integer
Dim j As Integer
'MsgBox Selection.Font.ThemeColor
On Error GoTo I_handle_error
Set cell0 = Cells(1, 1)
n = 45
For i = 0 To ActiveSheet.Range("A1").SpecialCells(xlCellTypeLas tCell).Row - 1
a:
cell0.Offset(i).Select
If Selection.Value = "total" Then
cell0.Offset(i, n - 1).Value = "end"
ElseIf Selection.Font.ThemeColor = xlThemeColorDark1 Then
cell0.Offset(i, n - 1).Value = "beg"
End If
Next i
Exit Sub
I_handle_error:
i = i + 1
GoTo a
End Sub
|