VBA FindNext not finding next

Alroj

New Member
Joined
Jan 12, 2016
Messages
42
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am having trouble with the script below and I am not sure what it is not correct.
The script looks for the word "EQ" in column J and then selects the 3 cells in front of the text (cells in column H,I,J) to cut and then paste them in column G. The script is only doing the first one but then I get the Run-time error '1004': Unable to get the FindNext property of the Range class

Could someone please assist shedding light on this issue

Cheers


VBA Code:
Sub FindAndCut()

   Dim C As Range
 

   With ActiveSheet.Range("j2:j20000")
        Set C = .Find("EQ", LookIn:=xlValues)
        
        If Not C Is Nothing Then
            firstAddress = C.Address
            Do
           Range(C, C.Offset(0, -2)).Select
             Selection.Cut Selection.Offset(0, -1)
             
           Set C = .FindNext(C)
        Loop While Not C Is Nothing And C.Address <> firstAddress
        End If
    End With

End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You are moving the cell you have found by the cut-paste action. This is:
• upsetting your find next since the cell is no longer in column J
• means you are never to return to the firstAddress, for the same reason.
see if this works for you.

VBA Code:
Sub FindAndCut()

    Dim C As Range
    Dim ws As Worksheet
    Dim rng As Range
    Dim lastRow As Long
    
    Set ws = ActiveSheet
    lastRow = ws.Range("J" & Rows.Count).End(xlUp).Row
    Set rng = ws.Range("J2:J" & lastRow)
           
    Set C = rng.Find("EQ", LookIn:=xlValues)
    
    If Not C Is Nothing Then
        Do
            ws.Cells(C.Row, C.Column - 3).Resize(, 3).Value = ws.Cells(C.Row, C.Column - 2).Resize(, 3).Value
            C.ClearContents
         
            Set C = rng.FindNext(C)
        Loop Until C Is Nothing
    End If

End Sub
 
Upvote 0
Solution
Hi Alex Blakenburg, this worked very well. Thank you for sharing your wisdom, I've learnt something new today

Cheers!

 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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