简体   繁体   中英

Using On Error Goto to try to find why I am getting an error

I am getting a type mismatch when I run my code. Is there a way to use On Error Goto to help me debug it? Here is my code.

Private Sub FH_CNC_HideOrders_Click()
    On Error GoTo errHandler

    If Me.FH_CNC_HideOrders.Caption = "Hide" Then
        'Intiansiate objects and setup variables
        Dim tbl As ListObject
        Dim c As Range
        Dim colStartDate As Range
        Dim FoundDate As Date
        'Set object/variable values
        Set tbl = ActiveWorkbook.Worksheets("Production Tracking").ListObjects("Table293")

        With tbl
            'Search "Start Date" (Col2), top to bottom, searching for the first cell with a color index of 15 and the "End Date" (Col3) which has an index color of anything other than 15
            Set colStartDate = .ListColumns("CNC Begins").DataBodyRange

            For Each c In colStartDate.Cells
                'MsgBox "c.Value:" & c.Value & "   |   c.Interior.ColorIndex:" & c.Interior.ColorIndex & "   |   c.Address:" & c.Address _
                    & Chr(10) & Chr(10) & "c.Offset.Value):" & c.Offset(0, 1).Value & "   |   c.Interior.ColorIndex:" & c.Offset(0, 1).Interior.ColorIndex & "   |   c.Address:" & c.Offset(0, 1).Address

                If c.Interior.ColorIndex = 15 And c.Offset(0, 1).Interior.ColorIndex <> 15 Then
                    FoundDate = c.Value
                    Exit For
                End If
            Next c

            For Each c In colStartDate.Cells

                If Not c.EntireRow.Hidden = True Then
errHandler:
                Msbox c.Value
                Exit Sub
                    'Hide dates prior to colStartDate but not empty cells
                    If Not IsEmpty(c.Value) Then
                        If Not c.Value >= FoundDate And IsDate(c.Value) Then


                            c.EntireRow.Hidden = True
                            'MsgBox c.Address
                        End If
                    End If
                End If
            Next c

        End With

        Me.FH_CNC_HideOrders.Caption = "Show"
    ElseIf Me.FH_CNC_HideOrders.Caption = "Show" Then
        Me.FH_CNC_HideOrders.Caption = "Hide"
    End If
End Sub

I have placed a comment in the code where I would like to MsgBox the value if the error happens.

You "can" but I really don't see why you "should".

If you' might get the mismatch error because c holds a non date, why not test c and find out?

If IsDate(c.Value) Then ...

Or maybe instead of testing if it's a date, find out what days type it is?

Select Case VarType(c.Value)
    Case 2 to 6
        MsgBox "These are not dates"
         Exit Sub
    Case 7
        c.EntireRow.Hidden = True
     Case Else
         ....

Or if you don't want to bother with those VBA constants...

If TypeName(c.Value) = "String" Then MsgBox "This is not a date"

Lets be clear about this. Error handling should not be used to find problems with your code. VBA already does a good job at this and will stop the program or refuse to compile.

Error handling is for exceptions, ie when you are using a resource outside of your control which may lead to your program crashing through no fault of your own. In this case you should trap the error and decide what to do with it.

To eliminate an logic errors in your code make sure you have done the following.

  1. Remove any on Error statements you have from your code.

  2. You have Option Explicit at the start of each module/class

  3. You can do Debug.Compile project without getting any errors.

  4. You have installed the fantastic RubberDuck addin and have corrected all of of the code Inspection results it finds.

You should now be left in a situation where the only errors you have do deal with are those caused by external events (exceptions).

For these final errors do not use the typical On error goto type of error handling you see in many examples (and as used in your code). Instead encapsulate the line which may generates an error between an On Error Resume Next and an On Error GoTo 0: thereby emulating the 'Try catch' structure seen in other well known programming languages.

This article has good information on best practise for error handling https://rubberduckvba.wordpress.com/2019/05/

Of the above No 4 is most likely where you will find any subtle errors you have made.

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM