What is wrong with my VBA code?

Lopez, Ahiezer 236 Reputation points
2022-07-18T22:50:30.617+00:00

I am trying to see if column C in the BackOrders Sheet has a value equal to the Active cell in the Active sheet, LINE. However, when running the code i get an object error. What am I doing wrong?

Object Error:
222023-image.png

Code:

  Application.ScreenUpdating = False  

  NumRows = Range("C1", Range("C1").End(xlDown)).Rows.Count  

  Range("C1").Select  

  For x = 1 To NumRows  
    Debug.Print ActiveCell  
      
Dim rng As Range  

        Set rng = BackOrders.Columns("C:C").Find(What:=Line.Cells(2, 3), _  
                        LookIn:=xlFormulas, _  
                        LookAt:=xlWhole, _  
                        SearchOrder:=xlByRows, _  
                        SearchDirection:=xlNext, _  
                        MatchCase:=False, _  
                        SearchForma:=False _  
                        )  
                    value = rng.Row  

     ActiveCell.Offset(1, 0).Select  
  Next  
  Application.ScreenUpdating = True  
Microsoft 365 and Office Excel For business Windows
Developer technologies Visual Basic for Applications
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nothing Left To Lose 396 Reputation points
    2022-07-20T00:28:35.033+00:00

    Try this instead...

    '---
    Sub FindSomething()
    'Nothing Left to Lose - July 2022
    Dim SearchFor As Long
    Dim rCell As Excel.Range
    Dim firstAddress As String
    Dim strMessage As String

    With Worksheets("BackOrders")
    SearchFor = .Cells(2, 3).Value
    Set rCell = .Cells.Find(SearchFor, LookIn:=xlValues)
    If Not rCell Is Nothing Then
    firstAddress = rCell.Address 'stopping point
    strMessage = rCell.Address(external:=True) 'includes sheet name
    Do
    Set rCell = .Cells.FindNext(rCell)
    'stop searching if
    If rCell Is Nothing Or rCell.Address = firstAddress Then Exit Do
    'accumulate locations
    strMessage = strMessage & vbCr & rCell.Address(external:=True)
    Loop
    End If
    End With
    VBA.MsgBox strMessage
    End Sub
    '---

    Nothing Left to Lose

    0 comments No comments

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.