Monday, January 4, 2010

Error Handler in VBA 7.0

Handling Error in form of programing language is vital task. To protect your application from stoping/freezing in midway is important. If you handle the error properly then you will not only successfully execute the application but you can also make user aware of the error which was thrown during the execution.


Lets us take a simple example for error handler:



Sub prTest()
'Error Handler Starts
On Error GoTo Err_PrTest:
Dim rngTest     As Range
Dim intTest     As Integer
Dim intCounter  As Integer

If rngTest(1, 1) = 0 Then
    'Do Nothing
 End If


Exit_PrTest:
    Set rngTest = Nothing
    Exit Sub
Err_PrTest:
    MsgBox Err.Description
    GoTo Exit_PrTest
End Sub


Once this code goes into error below error is thrown to the user:

No comments:

Post a Comment

Followers

About Me

My photo
Hello World!. I am an aspiring blogger, striving towards knowing more about various Technologies and innovating about it. I am full of Energy, Enthusiasm to learn more and more....