Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA: On Error Goto statement not working inside For-Loop

Tags:

I'm trying to cycle through a table in excel. The first three columns of this table have text headings, the rest of them have dates as headings. I want to assign those dates, sequentially, to a Date-type variable, and then perform some operations based on the date

To do this I am using a foreach loop on myTable.ListColumns. Since the first three columns do not have date headers, I have tried to set the loop up so that, if there is an error assigning the header string to the date-type variable, the loop goes straight to the next column

This seems to work for the first column. However, when the second column's header is 'assigned' to the date-type variable, the macro encounters an error even though it is within an error-handling block

Dim myCol As ListColumn
For Each myCol In myTable.ListColumns
    On Error GoTo NextCol

    Dim myDate As Date
    myDate = CDate(myCol.Name)

    On Error GoTo 0

    'MORE CODE HERE

NextCol:
    On Error GoTo 0
Next myCol

To reiterate, the error is thrown on the second round of the loop, at the statement

myDate = CDate(myCol.Name)

Can anyone explain why the On Error statement stops working?

like image 758
Swiftslide Avatar asked Aug 17 '12 01:08

Swiftslide


People also ask

What does On error GoTo mean in VBA?

On Error GoTo lineEnables the error-handling routine that starts at line specified in the required line argument. The line argument is any line label or line number. If a run-time error occurs, control branches to line, making the error handler active.

How do I stop a GoTo error?

To shut off (disable) the active handler, use On Error GoTo 0 . Doing so will close off the code block that uses that handler. Alternatively, exit the subroutine using Exit Sub , which automatically turns off the handler.

What is On error GoTo 0 in VBA?

VBA On Error GoTo 0 is an error handler statement used to disable the enabled error handler in the procedure. It is known as “Error Handler Disabler.” Error handling in any programming language is a master class that all the coders need to understand.


1 Answers

With the code as shown, you're actually still considered to be within the error handling routine when you strike the next statement.

That means that subsequent error handlers are not allowed until you resume from the current one.

A better architecture would be:

    Dim myCol As ListColumn
    For Each myCol In myTable.ListColumns
        On Error GoTo ErrCol
        Dim myDate As Date
        myDate = CDate(myCol.Name)
        On Error GoTo 0
        ' MORE CODE HERE '
NextCol:
    Next myCol
    Exit Sub ' or something '

ErrCol:
    Resume NextCol

This clearly delineates error handling from regular code and ensures that the currently executing error handler finishes before you try to set up another handler.

This site has a good description of the problem:


Error Handling Blocks And On Error Goto

An error handling block, also called an error handler, is a section of code to which execution is tranferred via a On Error Goto <label>: statement. This code should be designed either to fix the problem and resume execution in the main code block or to terminate execution of the procedure. You can't use the On Error Goto <label>: statement merely skip over lines. For example, the following code will not work properly:

    On Error GoTo Err1:
    Debug.Print 1 / 0
    ' more code
Err1:
    On Error GoTo Err2:
    Debug.Print 1 / 0
    ' more code
Err2:

When the first error is raised, execution transfers to the line following Err1:. The error hander is still active when the second error occurs, and therefore the second error is not trapped by the On Error statement.

like image 84
paxdiablo Avatar answered Sep 27 '22 22:09

paxdiablo