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
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You need to use Resume, not Continue
 
Upvote 0
Works fine for me. You don't really need error handling for this though - you should just test if the value was found before you try and use the found cell:

Code:
Dim foundCell as Range
set foundcell = Range("J2:J" & a).Find(What:=Cells(r, 1))
if not foundcell is nothing then
' process here
else
' do whatever you need to do for not found items here
end if
 
Upvote 0
Incidentally, why do you want this to run every time you select any cell on the sheet?
 
Upvote 0
Incidentally, why do you want this to run every time you select any cell on the sheet?
Don't need to, just seemed to be the simple thing to do. I need to take another look at the code.
 
Upvote 0
Now I remember, if a change is made to a cell, Worksheet_SelectionChange does not see that cell that was change because it has moved on and sees the current one and the code has to be looped to make sure all of them are updated.
 
Upvote 0
You should use Worksheet_Change if you want to monitor cells being changed.
 
Upvote 0
You should use Worksheet_Change if you want to monitor cells being changed.
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.
 
Upvote 0
@RoryA
Thanks for getting me out of a bind with "Nothing", here is my final code.

VBA Code:
Option Explicit
Public temp As String

Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Integer
' Clear cells on same row if "Delete" is pressed on column 1.
    For r = 2 To 24
        If Cells(r, 1) = "" And Cells(r, 4).Value <> "" Then
            Range(Cells(r, 2), Cells(r, 4)) = ""
        End If
    Next r
End Sub

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

    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)
        
        Set found = Range("J2:J" & a).Find(What:=Cells(r, 1))
        If Cells(r, 1) <> "" And found Is Nothing Then
            If temp <> item Then MsgBox Chr(34) & item & Chr(34) & " not found"
            temp = item
        Else
            If Not found Is Nothing Then
                Cells(r, 1) = Range(found.Address).Value
                Cells(r, 4) = Cells(Range(found.Address).Row, Range(found.Address).Column + 3)
                If Cells(r, 2) = "" Then Cells(r, 2) = 1
                Cells(r, 3) = Cells(r, 2) * Cells(r, 4)
            End If
        End If
    Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,516
Messages
6,125,285
Members
449,218
Latest member
Excel Master

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