Error Handler not activating

ndsutherland

Active Member
Joined
Jan 30, 2015
Messages
384
Code:
Private Sub InventoryList_LostFocus()
Dim i As Range
If ActiveSheet.Name = "Order Form" Then
    For Each i In ActiveSheet.Range("F2:F" & ActiveSheet.Range("F" & Rows.Count).End(xlUp).Row)
        On Error GoTo Other
        Cells(i.Row, 5) = Sheets("Inventory").Range("A" & WorksheetFunction.Match(i, Sheets("Inventory").Range("C:C"), 0))
        Cells(i.Row, 7) = Sheets("Inventory").Range("D" & WorksheetFunction.Match(i, Sheets("Inventory").Range("C:C"), 0))
        Cells(i.Row, 8) = Sheets("Inventory").Range("F" & WorksheetFunction.Match(i, Sheets("Inventory").Range("C:C"), 0))
        GoTo Either:
Other:
        On Error GoTo 0
        On Error GoTo ErrExit
[COLOR=#ff0000]        Cells(i.Row, 5) = Sheets("Other").Range("A" & WorksheetFunction.Match(i, Sheets("Other").Range("C:C"), 0))[/COLOR]
        Cells(i.Row, 7) = Sheets("Other").Range("D" & WorksheetFunction.Match(i, Sheets("Other").Range("C:C"), 0))
        Cells(i.Row, 8) = Sheets("Other").Range("F" & WorksheetFunction.Match(i, Sheets("Other").Range("C:C"), 0))
Either:
        Cells(i.Row, 1) = "SO-00000000"
        Cells(i.Row, 2) = "TBD"
    Next i
End If
ErrExit:
End Sub
My GoTo Other works just fine, but my code breaks (if match isn't found) on the highlighted line. What am I doing wrong?
code break setting set to "break on unhandled error" (I have tried "break in class module", not sure what that means, but it still doesn't work)
InventoryList is an ActiveX Comb Box
 
Last edited:

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,665
Office Version
365
Platform
Windows
Perhaps, without On Error.
Code:
Private Sub InventoryList_LostFocus()
Dim i As Range
Dim Res As Variant

    If ActiveSheet.Name = "Order Form" Then
        For Each i In ActiveSheet.Range("F2:F" & ActiveSheet.Range("F" & Rows.Count).End(xlUp).Row)

            Res = Application.Match(i, Sheets("Inventory").Range("C:C"), 0)
            If Not IsError(Res) Then
                Cells(i.Row, 5) = Sheets("Inventory").Range("A" & Res)
                Cells(i.Row, 7) = Sheets("Inventory").Range("D" & Res)
                Cells(i.Row, 8) = Sheets("Inventory").Range("F" & Res)
            Else
                Res = Application.Match(i, Sheets("Other").Range("C:C"), 0)
                If Not IsError(Res) Then
                    Cells(i.Row, 5) = Sheets("Other").Range("A" & Res)
                    Cells(i.Row, 7) = Sheets("Other").Range("D" & Res)
                    Cells(i.Row, 8) = Sheets("Other").Range("F" & Res)
                Else
                    Cells(i.Row, 1) = "SO-00000000"
                    Cells(i.Row, 2) = "TBD"
                End If
            End If

        Next i
    End If

End Sub
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,177
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
This might help explain it: On Error WTF? | Excel Matters

Essentially your On Error Goto 0 doesn't clear the current exception, so any further error is unhandled.
 

ndsutherland

Active Member
Joined
Jan 30, 2015
Messages
384
Excel kept crashing when I used Norie's code, so I went back to my original but changed "On Error GoTo 0" to "On Error GoTo -1" to clear the error, and it works perfectly. Norie, I did keep your Res idea to only match 2X instead of 6X. Thanks.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,862
Messages
5,489,315
Members
407,686
Latest member
Chuck1960

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top