VBA for Search for a Word, Cut Entire Row, & Paste on New Sheet

tmooreiii

New Member
Joined
Jun 24, 2022
Messages
3
Office Version
  1. 2021
Platform
  1. Windows
Hello All,
I am trying to create a Macro that will find specific words, then cut the row that the word(s) are found in & paste all that apply in a new sheet.
I searched the forum and found some codes, but none worked for me for what I wanted.
I know a little about how to do this but cannot manage to get it. I am good at more simplistic Macros. This one is tough for me.
In the image below I am trying to cut rows that contain the words in column J and paste in different sheets.

Is there someone who could get me something close?
 

Attachments

  • WORKLIST.JPG
    WORKLIST.JPG
    159.1 KB · Views: 12

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
in this code:
the sheet with the data is SHEET1
the sheet with the words to find is in: WORDS2FIND
(change to match yours)

then run FINDWORDS


Code:
Private mcolWords As New Collection
Private wsTarg As Worksheet, wsSrc As Worksheet
Private vWord

Sub FindWords()
On Error Resume Next
Sheets.Add
Set wsTarg = ActiveSheet
wsTarg.Name = "results"
'load the words list
Set mcolWords = New Collection
Sheets("words2Find").Activate
Range("A1").Select
While ActiveCell.Value <> ""
   vWord = ActiveCell.Value
   mcolWords.Add vWord, vWord
 
   ActiveCell.Offset(1, 0).Select  'next row
Wend

 'scan data sheet for the words. do it backwards to prevent row errors
Sheets("Sheet1").Activate
Set wsSrc = ActiveSheet
Range("A1").Select
Selection.End(xlDown).Select
While ActiveCell.Row > 1
   Find1Word
   ActiveCell.Offset(-1, 0).Select  'prev row
Wend
Set wsSrc = Nothing
Set wsTarg = Nothing
End Sub


private Function Find1Word()
Dim bFound As Boolean
Dim r, i
On Error GoTo ErrFind
bFound = False
r = ActiveCell.Row

Rows(r & ":" & r).Select
i = 1
While i <= mcolWords.Count And Not bFound
    vWord = mcolWords(i)
    Selection.Find(What:=vWord, After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
 
    bFound = True
    'MsgBox "found"
 
    'copy row to other sheet
    Rows(r & ":" & r).Select
    Selection.Cut
    wsTarg.Select
    ActiveSheet.Paste
    ActiveCell.Offset(1, 0).Select  'next row
      'back to source
    wsSrc.Select
    Rows(r & ":" & r).Select
    Selection.Delete Shift:=xlUp
tryNextWord:
    i = i + 1
Wend
Exit Function
ErrFind:
If Err = 91 Then
  GoTo tryNextWord
Else
  MsgBox Err.Description, , Err
End If
End Function
 
Upvote 0
Thanks.
I will give it a try this afternoon and update on how it worked for me.
I really appreciate it.
 
Upvote 0
I created the sheets (Tabs) and named them accordingly. I only got 1 result and 1 line of that.

What I am trying to do is find the words in the worklist and cut the rows and paste on individual worksheets (perhaps pre-named to the find words).
So, the code would search for rows with "Chem Film" in column J, cut entire row(s) from "WORLIST", and paste in a Worksheet named "Chem Film".
Is this an impossible task?
 

Attachments

  • Results.JPG
    Results.JPG
    13.8 KB · Views: 5
  • Tabs.JPG
    Tabs.JPG
    12 KB · Views: 6
  • Find Words.JPG
    Find Words.JPG
    32.8 KB · Views: 7
  • WORKLIST.JPG
    WORKLIST.JPG
    159.1 KB · Views: 6
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,510
Members
452,918
Latest member
Davion615

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