Use keyword to find word in table and copy and paste in new sheet.

NeedsomeHelp

New Member
Joined
Jul 12, 2022
Messages
6
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
Hi,

What I'm trying to do is use a keyword e.g. "Spotless" and with that keyword find any cell in the source data sheet with that keyword (could be in the middle of the word, end, start thats why it has wildcards) and if it has that keyword copy and paste the whole row in a new sheet.

The problem i have with the code now is that it will only work for one keyword How do i get it to work with multiple keywords say i want the keywords "spotless" "food" and "face" what would i need to do for the code to pick up all three keywords and copy and paste those in a new sheet.

If it is possible as well would i be able to use a different sheet named Keywords which would have a single column with different keywords in the column and the code would search for all the keywords in the column one after the other and copy and paste it in a new sheet.

VBA Code:
[

Sub Test1()
Application.ScreenUpdating = True
Dim xRow&, NextRow&, LastRow&
NextRow = 2
LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For xRow = 1 To LastRow

If WorksheetFunction.CountIf(Rows(xRow), "*Spotless*") > 0 Then
Rows(xRow).Copy Sheets("Sheet2").Rows(NextRow)
NextRow = NextRow + 1
End If
Next xRow
Application.ScreenUpdating = True
End Sub

]
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You've asked 2 very distinct questions there. In answer to the first, the following code makes the following assumptions due to the absence of any sample data:
  1. You are copying from "Sheet1" to "Sheet2"
  2. Your data in sheet 1 is in column A and starts in row 1
  3. Your key words could contain upper and lower case letters (as per your description)
Let's try this for a start on a copy of your data:

VBA Code:
Option Explicit
Option Compare Text

Sub Needsomehelp()
    Dim i As Long, lr1 As Long, lr2 As Long, c As Range, arr() As Variant
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    
    lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    lr2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row + 1
    
    For Each c In ws1.Range("A1:A" & lr1)
        If c.Value Like "*spotless*" Or _
        c.Value Like "*food*" Or _
        c.Value Like "*face*" Then
            ReDim Preserve arr(i)
            arr(i) = c.Value
            i = i + 1
        End If
    Next c
    
    With ws1.Range("A1").CurrentRegion
        .AutoFilter 1, Array(arr), 7
        .Offset(1).Copy ws2.Range("A" & lr2)
        .AutoFilter
    End With

End Sub
 
Upvote 0
EDITED
VBA Code:
Option Explicit
Option Compare Text

Sub Needsomehelp_2()
    Dim i As Long, lr1 As Long, lr2 As Long, c As Range, arr() As Variant
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    
    lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    lr2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row + 1
    
    For Each c In ws1.Range("A1:A" & lr1)
        If c.Value Like "*spotless*" Or _
        c.Value Like "*food*" Or _
        c.Value Like "*face*" Then
            ReDim Preserve arr(i)
            arr(i) = c.Value
            i = i + 1
        End If
    Next c
    
    With ws1.Range("A1").CurrentRegion
        .AutoFilter 1, Array(arr), 7
        .Copy ws2.Range("A" & lr2)
        .AutoFilter
    End With

End Sub
 
Upvote 0
Hi, when i try to run the edited code i get error 5 invalid procedure call on argument on line:

.AutoFilter 1, Array(arr) 7,
 
Upvote 0
Hi, when i try to run the edited code i get error 5 invalid procedure call on argument on line:

.AutoFilter 1, Array(arr) 7,
That is strange. The only difference was that I took out the Offset(1) from the first code because you seem to have data from row 1. Did the unedited code (post #2) work for you?
 
Upvote 0
Neither codes post #2 or #3 worked, same error on the same line.
OK, I'm flying blind here - both codes worked for me based on what I thought your data looked like. Before I look at this any further, I'll need to see your actual data. Please use the XL2BB Add in to provide it.
 
Upvote 0
This is an example of the source data i am looking at but the row categories go out to 20 and columns go down to 40K.
In this example the code would copy and paste rows 2, 3 and 4 into a new sheet.
DescriptionCodeLayoutTransactionCostAmountDocument NameFlavour
retrffhh
the love of foodfgu
7​
56​
ze
xvhj
8​
65​
Light Spotless Dirty Arear
saFacem
6​
435​
m
4​
ftrewrty
 
Upvote 0
You didn't use the XL2BB Add in as requested, but I've had a go based on your supplied image.
With this code:

VBA Code:
Option Explicit
Option Compare Text
Sub Needsomehelp_3()
    
    Dim i As Long, j As Long, lr1 As Long, lr2 As Long, lc As Long
    Dim c As Range, arr1, arr2
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")

    lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    lr2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row + 1
    lc = ws1.Cells(1, Columns.Count).End(xlToLeft).Column + 1
    
    arr1 = ws1.Range(ws1.Cells(2, 1), ws1.Cells(lr1, lc - 1))
    ReDim arr2(1 To lr1, 1 To 1)
    
    For i = LBound(arr1, 1) To UBound(arr1, 1)
        For j = LBound(arr1, 2) To UBound(arr1, 2)
            If arr1(i, j) Like "*spotless*" Or _
                arr1(i, j) Like "*food*" Or _
                arr1(i, j) Like "*face*" Then
                    arr2(i, 1) = 1
            End If
        Next j
    Next i
    
    ws1.Cells(2, lc).Resize(lr1 - 1).Value = arr2
    With ws1.Range("A1").CurrentRegion
        .AutoFilter lc, 1
        .Offset(1).Resize(, lc - 1).Copy ws2.Range("A" & lr2)
        .AutoFilter
    End With
    ws1.Columns(lc).ClearContents

End Sub

And this original sheet 1:
Spotless.xlsb
ABCDEFGH
1DescriptionCodeLayoutTransactionCostAmountDocument NameFlavour
2retrffhh
3the love of foodfgu756ze
4xvhj865Light Spotless Dirty Arear
5saFacem6435m4
6ftrewrty
Sheet1


And this original sheet 2:
Spotless.xlsb
ABCDEFGH
1DescriptionCodeLayoutTransactionCostAmountDocument NameFlavour
2
Sheet2


This is the result on sheet 2 after running the code:
Spotless.xlsb
ABCDEFGH
1DescriptionCodeLayoutTransactionCostAmountDocument NameFlavour
2the love of foodfgu756ze
3xvhj865Light Spotless Dirty Arear
4saFacem6435m4
5
Sheet2


If that doesn't work the same for you, then I really will need to see your actual data using the XL2BB Add in.
 
Upvote 0
Hi, it works!

Thankyou for being so patient and helping me out i really appreciate it.

Many thanks!
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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