Error 0 because of GoTo

awsumchillicrab

Board Regular
Joined
Jan 30, 2011
Messages
56
Hi,

I have a code that GoTo a place called "Err_TrimSpaces" in event of error during a loop. After the loop is complete, I get the error "0". I found that if I put an "Exit Sub" before the Err_TrimSpaces, then my code will avoid that error. So my question is, how do I get my code to skip Err_TrimSpaces once the loop is complete so I can proceed to process more code - written as ABCDE below?

sub test()

Dim LastRow As Integer
LastRow = Range("a1").CurrentRegion.Rows.Count

Range("e1").Select
On Error GoTo Err_TrimSpaces
Do Until ActiveCell = Range("E" & LastRow)
ActiveCell = Trim(ActiveCell)
ActiveCell = Int(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop

Exit Sub

Err_TrimSpaces:
If Err.Number = 13 Then 'data mismatch ie not number
Resume Next
Else
MsgBox Err.Number
End If

ABCDE

end sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hop over it

Code:
sub test()
Dim LastRow As Integer
LastRow = Range("a1").CurrentRegion.Rows.Count
Range("e1").Select
On Error GoTo Err_TrimSpaces
Do Until ActiveCell = Range("E" & LastRow)
ActiveCell = Trim(ActiveCell)
ActiveCell = Int(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
[COLOR=blue]Goto SkipErrTrap[/COLOR]
Err_TrimSpaces:
If Err.Number = 13 Then 'data mismatch ie not number
Resume Next
Else
MsgBox Err.Number
End If
[COLOR=blue]SkipErrTrap:[/COLOR]
ABCDE
end sub
 
Upvote 0
Also, you can just put ABCDE after the loop.

Code:
Sub test()
Dim LastRow As Integer

    LastRow = Range("a1").CurrentRegion.Rows.Count
    
    Range("e1").Select
    On Error GoTo Err_TrimSpaces
    
    Do Until ActiveCell = Range("E" & LastRow)
        ActiveCell = Trim(ActiveCell)
        ActiveCell = Int(ActiveCell)
        ActiveCell.Offset(1, 0).Select
    Loop
    
    ABCDE
    
    Exit Sub

Err_TrimSpaces:
If Err.Number = 13 Then 'data mismatch ie not number
    Resume Next
Else
    MsgBox Err.Number
End If

End Sub

However, you don't need the error handling necessarily:
Code:
Sub test()
Dim LastRow As Integer

    LastRow = Range("a1").CurrentRegion.Rows.Count
    
    Range("e1").Select
    
    Do Until ActiveCell = Range("E" & LastRow)
        If Not IsNumeric(ActiveCell) Then
            Trim (ActiveCell)
        End If
        If IsNumeric(ActiveCell) Then
            ActiveCell = Int(ActiveCell)
        End If
        ActiveCell.Offset(1, 0).Select
    Loop


End Sub

Or ... you can use "inline" error handling:
Code:
Sub test2()
Dim LastRow As Integer

    LastRow = Range("a1").CurrentRegion.Rows.Count
    
    Range("e1").Select
    
    On Error Resume Next
    Do Until ActiveCell = Range("E" & LastRow)
        ActiveCell = Trim(ActiveCell)
        ActiveCell = Int(ActiveCell)
        ActiveCell.Offset(1, 0).Select
    Loop
    On Error GoTo 0

End Sub

ξ
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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