Search cell content for particular word, if found copy row

spectraflame

Well-known Member
Joined
Dec 18, 2002
Messages
829
Office Version
  1. 365
Platform
  1. Windows
I have imported a text file into Excel. Column B contains a text string that varies in lengh from blank up to 50 characters. I need to be able to search the values in that column and copy the entire row to another sheet within the same workbook if the cell contains the word REMOTE.

Is this possible?

Thanks,
Matthew
 

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.
Re: Search cell content for particular word, if found copy r

Try this:
This macro will look for the word "REMOTE" in each cell of Column B on Sheet1 and if it is there will then write the entire row into Sheet2 starting at A1.

Sub moveremote()
Dim x As Long, y As Long
y = 1
Application.ScreenUpdating = False
For x = 1 To Worksheets("Sheet1").Range("B65536").End(xlUp).Row
If InStr(1, Worksheets("Sheet1").Range("B" & x), "REMOTE") > 0 Then
Worksheets("Sheet1").Range("B" & x).EntireRow.Copy
Worksheets("Sheet2").Range("A" & y).PasteSpecial
Application.CutCopyMode = False
y = y + 1
End If
Next x
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Re: Search cell content for particular word, if found copy r

Thanks HOTPEPPER!

That works just like I needed it to.

Matthew
 
Upvote 0
Sorry to intrude but
I have a need for something like this.

How would I modify it to use Column A and then delete the entire row
from Sheet1 after the text string is found and pasted in Sheet 2?

Thanks.

compressor
 
Upvote 0
How would I modify it to use Column A and then delete the entire row
from Sheet1 after the text string is found and pasted in Sheet 2?

ASSUMPTIONS:
- sheet to copy from is active
- criterial column is column B
- Header row has values AND is row 1:1 :eek:

FEATURES
- This solution has no loops to slow process down


Code:
Public Sub DEMO()
Dim Src As String
Src = ActiveSheet.Name

    'MAKE COPY OF ALL ROWS WITH "REMOTE" IN COL B
    CopyByCriteria ("REMOTE")
    
    Sheets(Src).Activate
    
    'MAKE COPY OF ALL ROWS WITH "REMOTE" NOT IN COL B
    DeleteByCriteria ("REMOTE")
    
End Sub


Sub CopyByCriteria(Crit)
Dim SrcRange As Range
Dim CritRange As Range

Set SrcRange = Range("A1:Z65536")
Set CritRange = Range("IV1:IV2")

'Establish Criteria
    CritRange(1).ClearContents
    CritRange(2).Formula = "=FIND(" & Chr(34) & Crit & Chr(34) & ",B2,1)>0"
  
'Create Target Sheet
    Sheets.Add

'Use Advanced Filter to copy data that meets criteria
    SrcRange.AdvancedFilter Action:=xlFilterCopy, _
    CriteriaRange:=CritRange, _
    CopyToRange:=Range("A1"), Unique:=False
    
' Clear Criteria
   CritRange.ClearContents
End Sub


Sub DeleteByCriteria(Crit)
Dim SrcRange As Range
Dim CritRange As Range

Set SrcRange = Range("A1:Z65536")
Set CritRange = Range("IV1:IV2")

'Establish Criteria
    CritRange(1).ClearContents
    CritRange(2).Formula = "=iserror(FIND(" & Chr(34) & Crit & Chr(34) & ",B2,1)>0)"
  
'Create Target Sheet
    Sheets.Add

'Use Advanced Filter to copy data that meets criteria
    SrcRange.AdvancedFilter Action:=xlFilterCopy, _
    CriteriaRange:=CritRange, _
    CopyToRange:=Range("A1"), Unique:=False
    
' Clear Criteria
   CritRange.ClearContents
End Sub
 
Upvote 0
VERSION 2

FEATURES
- This solution has no loops to slow process down



Sub DEMO2()
Dim Src As String
Src = ActiveSheet.Name

'MAKE COPY OF ALL ROWS WITH "REMOTE" IN COL B
CopyByCriteria ("=FIND(""REMOTE"",B2,1)>0")

Sheets(Src).Activate

'MAKE COPY OF ALL ROWS WITH "REMOTE" <NOT> IN COL B
CopyByCriteria ("=iserror(FIND(""REMOTE"",B2,1)>0)")

End Sub


Sub CopyByCriteria(Crit)
Dim SrcRange As Range
Dim CritRange As Range

Set SrcRange = Range("A1:Z65536")
Set CritRange = Range("IV1:IV2")

'Establish Criteria
CritRange(1).ClearContents
CritRange(2).Formula = Crit

'Create Target Sheet
Sheets.Add

'Use Advanced Filter to copy data that meets criteria
SrcRange.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=CritRange, _
CopyToRange:=Range("A1"), Unique:=False

' Clear Criteria
CritRange.ClearContents
End Sub
 
Upvote 0
Thanks Nimrod! Both seem to work.
But the macro puts the resulting Sheets before Sheet1.

Not a big deal but can it keep the Sheets in order?

What do I change to make it look at Column A of Sheet1?

Now if I may take this a step further.

Ideally I would like to search for multiple text strings
in Column A, put each on a seperate sheet, delete from the original,
and be able to add more text strings to the search
as needed in the future.
The starting file will be one sheet with less than 10000 rows and upto
20 columns. The search would always be based on Column A text strings.

Could you help with this?

compressor
 
Upvote 0
Not a big deal but can it keep the Sheets in order?

Code:
Public Sub NewSheetAfterCurrSh()
    ' new sheet behind current sheet
    Sheets.Add after:=ActiveSheet
End Sub

Public Sub NewSheetAtVeryEnd()
    ' new sheet behind all sheets
    Sheets.Add after:=Sheets(Sheets.Count)
End Sub
 
Upvote 0
What do I change to make it look at Column A of Sheet1?

If your using VERSION 2 then just change it to this ...

Code:
Sub DEMO2()
Dim Src As String

Src = "Sheet1"
Sheets(Src).Activate


'MAKE COPY OF ALL ROWS WITH "REMOTE" IN COL B
CopyByCriteria ("=FIND(""REMOTE"",A2,1)>0")

Sheets(Src).Activate

'MAKE COPY OF ALL ROWS WITH "REMOTE" IN COL B
CopyByCriteria ("=iserror(FIND(""REMOTE"",A2,1)>0)")

End Sub

NOTE:
- change 1 was the addition of what sheet to select
- change 2 was the B2 , in the criteria, was change to A2
 
Upvote 0
Ideally I would like to search for multiple text strings
in Column A, put each on a seperate sheet, delete from the original,
and be able to add more text strings to the search
as needed in the future.
The starting file will be one sheet with less than 10000 rows and upto
20 columns. The search would always be based on Column A text strings.

CONFIG :
This line is what you modify for you various search strings:
For Each SrchString In Array("REMOTE", "TOM", "****", "HARRY")

Assumptions
- Source Sheet is active
- Criterial Column is Column A


Code:
Sub DEMO3()
Dim Src As String
Src = ActiveSheet.Name

For Each SrchString In Array("REMOTE", "TOM", "****", "HARRY")
    

    'MAKE COPY OF ALL ROWS WITH STRING IN COL B
    CopyByCriteria ("=FIND(" & Chr(34) & SrchString & Chr(34) & ",A2,1)>0")
    
    Sheets(Src).Activate
    
    'MAKE COPY OF ALL ROWS WITH "REMOTE" IN COL B
    CopyByCriteria ("=iserror(FIND(" & Chr(34) & SrchString & Chr(34) & ",A2,1)>0)")
    
    'MAKE NEW COPY THE SOURCE SHEET
    Application.DisplayAlerts = False
    Sheets(Src).Delete
    ActiveSheet.Name = Src
    Application.DisplayAlerts = True
    
Next SrchString

End Sub


Sub CopyByCriteria(Crit)
Dim SrcRange As Range
Dim CritRange As Range

    Set SrcRange = Range("A1:Z65536")
    Set CritRange = Range("IV1:IV2")
    
    'Establish Criteria
    CritRange(1).ClearContents
    CritRange(2).Formula = Crit
    
    'Create Target Sheet
    Sheets.Add after:=ActiveSheet
    
    'Use Advanced Filter to copy data that meets criteria
    SrcRange.AdvancedFilter Action:=xlFilterCopy, _
    CriteriaRange:=CritRange, _
    CopyToRange:=Range("A1"), Unique:=False
    
    ' Clear Criteria
    CritRange.ClearContents
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,276
Members
449,149
Latest member
mwdbActuary

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