On Error fails

jblevins

Active Member
Joined
Sep 2, 2013
Messages
250
Office Version
  1. 2003 or older
When the first bad item in the list is found, everything works, but if a second one is found "On Error" fails and the VBA error "Run-time error 91': Object variable or With block variable not set" occurs. "On Error Goto 0" does not reset the error.

VBA Code:
Option Explicit
Public i
Public temp

Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim r
Dim a
Dim item
Dim found

    a = 2
    While Cells(a, 10) <> ""
        a = a + 1
    Wend
    a = a - 1
    
    For r = 2 To 24
        If Cells(r, 1) <> "" Then
            item = Cells(r, 1)
            On Error GoTo baditem
            found = Range("J2:J" & a).Find(What:=Cells(r, 1)).Address
            Cells(r, 1) = Range(found).Value
            Cells(r, 4) = Cells(Range(found).Row, Range(found).Column + 3)
            If Cells(r, 2) = "" Then Cells(r, 2) = 1
            Cells(r, 3) = Cells(r, 2) * Cells(r, 4)
        End If
continue:
    Next r

    For r = 2 To 24
        If Cells(r, 1) = "" Then
            Range(Cells(r, 2), Cells(r, 4)) = ""
        End If
    Next r
    
    Exit Sub

baditem:
    If temp = item Then GoTo continue
    If i = 0 Then
        MsgBox Chr(34) & item & Chr(34) & " was not found"
        i = 1
    Else
        i = 0
    End If
    temp = item
End Sub
 
Same issue, when I move from the change cell, Change moves to the current cell and leaves the changed cell, cannot trap the changed cell address.
The Target argument supplied by Worksheet_Change refers to the cell(s) that changed.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Forum statistics

Threads
1,215,650
Messages
6,126,019
Members
449,280
Latest member
Miahr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top