Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax to determine current error handling method in VBA

Tags:

vba

On any given line in my code, can I use Debug.Print() together with some other command to find out what would happen if my code would encounter an error on that line?

Can I use any statement within Debug.Print to find out if the current error handling procedure is to Goto 0, Resume Next or Goto some label?

I'm envisioning something like:

Debug.Print(OnErrorMode)

I am looking for a way to determine whether a specific line of code that errors will GoTo 0 (stop and show error), Resume Next (skip the line), or GoTo Label (jump to Label) given that an On Error might be buried somewhere in my code that I cannot find. Is that possible?

like image 725
user1283776 Avatar asked Jan 29 '15 15:01

user1283776


People also ask

What are the 3 different types of error handling techniques in VBA?

AutoCAD to Excel - VBA Programming Hands-On! There are three types of errors in programming: (a) Syntax Errors, (b) Runtime Errors, and (c) Logical Errors.

Can I use today () in VBA?

Today means the current date. In the worksheet, the NOW function does the same thing, which gives us the current date and time, but there is no built-in Today function in VBA. So instead, the method to get the system's current date is by using the Date function.

How do you check if a cell has an error VBA?

The VBA ISERROR function is listed under the information category of VBA functions. When you use it in a VBA code, it evaluates the supplied expression and returns TRUE if it is an error else FALSE. In simple words, it can check whether the value supplied is an error or not and returns TRUE or FALSE based on that.


1 Answers

In over a decade of using VBA, and VB (5 and 6), I have never seen a single reference that even remotely suggests that one may be able to determine what error trapping conditions exists. While the compiler surely knows, it is not exposed.

It seems to me that your problem may be in regards to how you are structuring your error trapping.

I wrap subroutines in a single On Error Goto statement, located in the same subroutine as the On Error. I have an exit and when the error occurs, I log what happened so I can fix the bug. I then can use debug.print or create a string with debugging information that is appended to the log.

Sub main()
         On Error GoTo Main_Error
         'Insert Code Here
         Exit Sub
         Main_Error:
         WriteLog Err.Number, Err.Description
        MsgBox ("The program encoutnered an error.")
    End Sub
    Private Sub WriteLog(ErrNum As Integer, ErrDes As String)
        Open "Errors.txt" For Append As #1
        Print #1, Now & "  " & ErrNum & " - " & ErrDes
        Close #1 
    End Sub

The second pattern I use is in areas in which there is a good chance that an error may occur, like opening a file (files might be missing), or a database. then I use the error trapping like a try catch block like so.

Private Function LoadFile() As Boolean
    On Error GoTo Main_Error
    'Code before a potentially error-prone statement
    On Error Resume Next
    Open "aMissingFile.txt" For Input As #1
    If Err.Number <> 0 Then
        MsgBox ("aMissingFile.txt is missing")
        LoadFile = False
        Exit Function
    End If
    On Error GoTo LoadFile_Error
    'The rest of your code
    LoadFile = True
    Exit Function
LoadFile_Error:
    WriteLog Err.Number, Err.Description
    MsgBox ("The program encoutnered an error.")
    LoadFile = False
End Function

This makes dealing with errors easier and debugging is as easy as commenting out a single On error statement. Note that err.number lists the error code return, so you can add logic to handle different types of errors.

like image 155
Andrew Neely Avatar answered Nov 01 '22 20:11

Andrew Neely