Why am I getting an Object variable error?

Lopez, Ahiezer 236 Reputation points
2022-10-07T15:51:37.95+00:00

I am trying to see if the Part Number in column AA exists in column U. If so I want column AB to say YES, and I want excel to find that particular part number in column U, wherever it may be, and highlight it. I copied some values from column U onto column AA to see if its working fine and it is but when the code encounters values that are typed I get an Object variable or with block variable not set.

248558-image.png
248545-image.png

248430-image.png

Code:
` Dim x As Integer
Application.ScreenUpdating = False
' Set numrows = number of rows of data.
NumRows = Range("AB1", Range("AB1").End(xlDown)).Rows.Count
' Select cell a1.
Range("AB1").Select
Dim rngS As Range

 Dim rngFound As Range  
 Set rngS = Cells.Range("U:U")  
  ' Establish "For" loop to loop "numrows" number of times.  
  For x = 1 To NumRows  
  Debug.Print (x)  
  Debug.Print (Cells(x, 28))  
  If Cells(x, 28) = "YES" Then  
    Debug.Print (Cells(x, 27))  
      
    Set rngFound = rngS.Find(Cells(x, 27), , xlFormulas, xlWhole)  

' Debug.Print (highlighted_value)

' Debug.Print (highlighted_value.Column)
' Debug.Print (highlighted_value.Row)
rngFound.Interior.Color = vbBlue
'

'=NOT(ISERROR(VLOOKUP(TRIM(U21:U36),TRIM($AA$2:$AA$12),1,FALSE)))
'=NOT(ISERROR(VLOOKUP(TRIM(U4:U17),TRIM($AA$2:$AA$12),1,FALSE)))
' Debug.Print (highlighted_value)

    'find cell value in column U  
    
  End If  
    
     ' Insert your code here.  
     ' Selects cell down 1 row from active cell.  
     ActiveCell.Offset(1, 0).Select  
  Next  
  Application.ScreenUpdating = True  

`

Microsoft 365 and Office Development Other
Developer technologies Visual Basic for Applications
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2022-10-07T16:12:31.727+00:00

    Probably some of the values are not found. Try adding a condition:

    If Not rngFound Is Nothing Then  
        rngFound.Interior.Color = vbBlue  
    End If  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.