Creating VBA Macro that Selects certain words and then copies entire row into seperate sheet

alef1234

New Member
Joined
Nov 12, 2015
Messages
17
I am trying to create a macro that will look in a particular column (say column A) and search for certain text that I define (such as the word "one day") and if the cell has that words, the macro would slect that entire row and paste the row into a seperate spread sheet. I tried using Active.Cell= but that did not help. Any help is greatly appreciated.

My spreadsheet has about 100 rows that I want the macro to look through.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
The general code is as follows - it searches for text "one day". In real world, I guess, you have several words to search - then you'd need to iterate over each one.
Code:
Sub FindWords()


    Dim wksSource As Worksheet
    Dim wksTarget As Worksheet
    Dim cell As Range
    
    Set wksSource = Sheets("YOUR_SOURCE_SHEET_NAME")
    Set wksTarget = Sheets("YOUR_TARGET_SHEET_NAME")


    ' The main is xlPart - it searches for incomplete entries (opposite to xlWhole)
    Set cell = wksSource.Columns(1).Find(What:="one day", LookAt:=xlPart)


    ' If cell variable is not nothing then - we've found a cell.
    ' Copy it to target worksheet (in this example - A1 cell).
    If Not cell Is Nothing Then
        cell.Copy wksTarget.Range("A1")
    End If
    
End Sub
 
Upvote 0
This is great. How would I be able to adjust the code so that after is searches and find one set of words I am looking for (one day) and paste into separate sheet, thee macro will go back and look in clomun A for another row that contains some other text (for example maybe (green house) and take the entire row with text that containst green house and paste it into the same sheet but in the following row as we pasted in the row containing "one day" text.
 
Upvote 0
As I said, you'd need to set up a column with the values being searched. Say, it'd be column B. Then B1 will containg one text, B2 - another text etc.
Then the code would look like this:
Code:
Sub FindWords()


    Dim wksSource As Worksheet
    Dim wksTarget As Worksheet
    Dim cell As Range
    Dim r, w
    
    Set wksSource = Sheets("YOUR_SOURCE_SHEET_NAME")
    Set wksTarget = Sheets("YOUR_TARGET_SHEET_NAME")


    With wksSource
        'Iterate over each cell with text in colum B
        For r = 1 To Cells(Rows.Count, "B").End(xlUp).Row 'last row in column B
            ' The main is xlPart - it searches for incomplete entries (opposite to xlWhole)
            Set cell = .Columns(1).Find(What:=Cells(r, "B").Value, LookAt:=xlPart)
            ' If cell variable is not nothing then - we've found a cell.
            ' Copy it to target worksheet (in this example - A1 cell).
            If Not cell Is Nothing Then
                w = w + 1 'Shift 1 row down in target sheet
                cell.Copy wksTarget.Cells(w, "A")
            End If
        Next
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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