Error capture while using .Find is not identifing

2020-05-07 08:23发布

When .Find does not find a result, I want an error msg. I have used the method that is almost universally recommended online, but it is not working. When a value is not found, nothing happens. There should be a msg box identified the error.

If Not rFoundCell Is Nothing Then
  MsgBox "val: " & rValue.Value & "   Matching Cell: " &     rFoundCell.Address
  Cells(Range(rFoundCell.Address).Row,     Range(rFoundCell.Address).Column).Select
Else
   MsgBox (rValue.Value & " not found.")
   GoTo end_search
End If  

I've tried the other way as well:
If rFoundCell Is Nothing Then
Display a msg "not found"
else
Keep going.
That didn't work either. What am i missing?
Full code follows:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim PostRng As Range
Dim PendRng As Range
Dim rValue As Range
Dim lLoop As Long
Dim rFoundCell As Range
Dim INTRng As Range

Set PostRng = Range("g:g")
Set PendRng = Range("k:k")

'"Intersect" will ensure your current cell lies on correct column.
Set INTRng = Intersect(Target, PostRng)

'IF conditions to trigger code.
'This IF confirms only one cell changed. -- I think
If Target.Columns.Count = 1 And Target.Rows.Count = 1 Then
    If Not INTRng Is Nothing And LCase(Target.Text) = "y" Then
'This block will return the range & value on the row where "y" or "Y" are entered.
        Set rValue = Target.Offset(0, -3)        'Returns value in Col D
        If rValue = 0 Or rValue = "" Then Set rValue = Target.Offset(0, -2)
Debug.Print "Target "; Target
Debug.Print "rvalue.value "; rValue.Value

'This will loop through a different column, to find the value identified above, and return its cell address in the other column.
      With PendRng
        Set rFoundCell = .Cells(1, 1)
        For lLoop = 1 To WorksheetFunction.CountIf(.Cells, rValue.Value)
            Set rFoundCell = .Find(What:=rValue.Value, _
               After:=rFoundCell, _
               LookIn:=xlValues, _
               LookAt:=xlPart, _
               SearchOrder:=xlByRows, _
               SearchDirection:=xlNext, _
               MatchCase:=False)
Debug.Print "rfoundcell " & rFoundCell
        If Not rFoundCell Is Nothing Then
            MsgBox "val: " & rValue.Value & "   Matching Cell: " &     rFoundCell.Address
'This will use the cell address identified above to move the active cell to that address.
'Have to convert the address to row/column to use in Cell.Select.
        Cells(Range(rFoundCell.Address).Row,     Range(rFoundCell.Address).Column).Select
        Else
            MsgBox (rValue.Value & " not found.")
            GoTo end_search
        End If
        Next lLoop
        End With
    End If
End If
end_search:
End Sub

Received help w/ this code here:

Execute a subroutine when a user enters a trigger into a cell

1条回答
啃猪蹄的小仙女
2楼-- · 2020-05-07 08:54

I believe that your code is skipping the If statement that generates the error box if there is not a match.

This is due to For lLoop = 1 To WorksheetFunction.CountIf(.Cells, rValue.Value) exiting when there is no matches because it equates to For lLoop = 1 To 0

I moved all of your error message code into an If statement above the lLoop as follows:

    If WorksheetFunction.CountIf(.Cells, rValue.Value) = 0 Then
        MsgBox (rValue.Value & " not found.")
        GoTo end_search
    End If
查看更多
登录 后发表回答