Andyatwork
Board Regular
- Joined
- Mar 29, 2010
- Messages
- 94
Hi all,
I've cobbled together the below code from a variety of sources and it is falling over after the first loop.
The ultimate goal is to load an array with a bunch of different search terms, check a given range for the appearance of any one of those terms and if found, cut the affected row and dump it into sheet(2) before looping round until all search terms have been checked.
This code is a proof of concept to get it to work for one search term before trying to bodge an array in there as well.
I get a "run time 1004 unable to get the FindNext property of the Range class" error after the first loop and I can't figure out why.
I'm also not sure of the syntax for fitting shift:=xlup into the code after the found row is cut and dumped into the next worksheet.
Is it something to do with cutting the Rng? I don't really understand the find/findnext terms yet.
Any guidance or solutions would be very welcome.
Regards,
Andy
I've cobbled together the below code from a variety of sources and it is falling over after the first loop.
The ultimate goal is to load an array with a bunch of different search terms, check a given range for the appearance of any one of those terms and if found, cut the affected row and dump it into sheet(2) before looping round until all search terms have been checked.
This code is a proof of concept to get it to work for one search term before trying to bodge an array in there as well.
I get a "run time 1004 unable to get the FindNext property of the Range class" error after the first loop and I can't figure out why.
I'm also not sure of the syntax for fitting shift:=xlup into the code after the found row is cut and dumped into the next worksheet.
Code:
Option Explicit
Sub Copy_To_Another_Sheet_1()
Dim FirstAddress As String
Dim MyArr() As Variant 'array containing search terms
Dim Rng As Range 'found cell containing search term
Dim Rcount As Long, I As Long, lRow As Long
Dim NewSh As Worksheet 'dumping ground for cut rows
' With Application
' .ScreenUpdating = False
' .EnableEvents = False
' End With
' Fill in the search Value
MyArr = Array("Deceased")
Set NewSh = Sheets(2)
' set last row parameter of data
With Sheets(1)
lRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With Sheets(1).Range("h2:h" & lRow)
Rcount = 0
For I = LBound(MyArr) To UBound(MyArr)
Set Rng = .Find(What:=MyArr(I), _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
FirstAddress = Rng.Address
Do
Rcount = Rcount + 1
Rng.EntireRow.Cut NewSh.Range("A" & Rcount)
Set Rng = .FindNext(Rng)
[COLOR=#ff0000]Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress ' ERROR HERE
[/COLOR] End If
Next I
End With
' With Application
' .ScreenUpdating = True
' .EnableEvents = True
' End With
End Sub
Is it something to do with cutting the Rng? I don't really understand the find/findnext terms yet.
Any guidance or solutions would be very welcome.
Regards,
Andy