FindNext property of Range Object cannot be assigned

jason009

New Member
Joined
Jan 7, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have written a code, which includes the FindNext method. All the code works so far, only when it gets to the FindNext method it shows an error saying the FindNext Object cannot be assigned. However, I don't see where the Range Object (in this case "cell") is changed in any way for the FindNext method to not be able to assign it. Has anybody got an idea?

Please ignore any chunky written code, I'm very new with VBA ;)

VBA Code:
Sub Search()

    Dim wks As Worksheet
    Set wks = ActiveSheet
    
    Dim cell As Range, sngCell As Range
    Dim Count As Long
    Dim firstCell As String
    
    Dim rg As Range, lastColumn As Long
    
    Set cell = wks.Cells.Find(what:="Planned Supply at BP|SL (EA)", LookIn:=xlValues, lookat:=xlWhole)
    
    firstCell = cell.Address
    
    Do
    
    With wks
        lastColumn = .Cells(cell.Row, .Columns.Count).End(xlToLeft).Column
        Set rg = Range(cell, .Cells(cell.Row, lastColumn))
    End With
    
    For Each sngCell In rg
    If IsNumeric(sngCell.Value) = True Then
        If sngCell.Value > 0 Then
            If sngCell.Offset(-2, 0).Value > sngCell.Value Then
            Count = Count + 1
            End If
            If Count = 0 Then
            Rows(sngCell.Row - 2).Delete
            Rows(sngCell.Row - 1).Delete
            Rows(sngCell.Row).Delete
            End If
        End If
    End If
    Next
    
    Set cell = wks.Cells.FindNext(cell)
    
    Loop While cell.Address <> firstCell

End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
sngCell is part of range rg (1 row range) which includes the range cell.

VBA Code:
Set rg = Range(cell, .Cells(cell.Row, lastColumn))

So if you delete the entire row

VBA Code:
Rows(sngCell.Row).Delete

then cell also gets deleted and hence your error message
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,165
Members
452,615
Latest member
bogeys2birdies

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