Find keywords and post on different sheets

JoeDelcambre

New Member
Joined
Feb 20, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
I am trying to write a macro (possible 2 macros) to find 2 sets of keywords on Sheet 3.

First query/macro:
Search Sheet 3 only.
Sheet 4 can only have terms from "Set 1". If the query finds a line where there are words from only Set 1, results should be pasted on Sheet 4.
If the query finds a row with words from both Sets 1 and 2, those lines should pasted into Sheet 6.

The keywords are:
Set 1
"abc"
"def"
"xyz"


Second query/macro:
Search Sheet 3 only.
Sheet 5 can only have terms from "Set 2". If results from this query finds a line with only words from Set 2, results should be pasted on Sheet 5. If the query finds a row with words from both Sets 1 and 2, those lines should pasted into Sheet 6.
Set 2
"apple"
"banana"
"orange"


Sheet 6 can only have lines with at least one term from both Sets 1 and 2.

Any and all help is greatly appreciated.


macro tests2.xlsm
ABCDEFGHIJKLMN
11IDMust_PayFundedAgencyDirectorate_IDDivision_IDAcq_PackageOS Amount_Paid Amount_Owed Date_UpdatedStart_DateEnd_DateUpdated_By
12SDJ4478yesnoUSMCJ6CSDUSMC-Package 789XP$ 2,000.00$ 12,000.0011/15/20212/5/20202/2/2022Jane Doe
13LIU9898noyesUSAJ3CSDUSA-Package 3398JTTP4 Windows 7 HWLM$ 3,500.00$ 6,500.003/2/20213/1/20212/3/2022John Doe
14LKJ2147noyesUSAFJ3CSDUSAF-Package 776534Win XP$ 2,100.00$ 3,500.007/25/20217/5/20212/7/2022Jane Smith
15POI6698yesnoUSAJ4IODUSA-Package 3398Jwin xp$ 9,800.00$ 12,000.009/2/20214/16/20202/8/2022Jane Smith
16ABCS987nonoUSAFJ1SDDUSAF-Package 776534TTP4 Windows 7 HWLM$ 7,400.00$ 8,000.006/6/20213/1/20212/9/2022Joe Doe
17SMN3298noyesUSSFS4CSDUSSF-Package 2312Windows 7$ 6,500.00$ 9,500.009/22/20217/5/20212/10/2022Joe Doe
18LMN326yesyesUSCGS2CSDUSCG-Package 3287Windows XP$ 2,100.00$ 2,500.007/9/20213/1/20212/12/2022John Doe
19WDC589noyesUSNS6CIOUSN-Package 923Win 7$ 1,750.00$ 8,700.002/1/202211/9/20202/13/2022Jane Doe
20VFD365yesyesUSNS6CIOUSN-Package 923Win xp$ 630.00$ 6,500.003/6/20213/25/20202/14/2022Jane Smith
21CVF127yesyesUSMCS3CIOUSMC-Package 78974BBN win 7 TTP77$ 2,500.00$ 3,500.0012/1/20211/6/20212/15/2022Jane Smith
22CVRF9863yesnoUSNJ2SDDUSN-Package 923windows 7$ 7,800.00$ 7,800.002/2/20229/7/20202/16/2022Jane Smith
23EWD441yesyesUSAFS4CSDUSAF-Package 776534windows XP$ 1,200.00$ 4,500.008/5/20217/7/20202/18/2022John Doe
24RRB2285nonoUSAFS5CSDUSAF-Package 776534Windows 7$ 3,200.00$ 4,500.009/5/20219/3/20202/19/2022John Doe
25SDJ4478yesorangeUSMCJ6CSDUSMC-Package 789XP$ 2,000.00$ 12,000.0011/15/20212/5/20202/2/2022Jane Doe
26LIU9898noyesUSAJ3xyzUSA-Package 3398JWindows 7$ 3,500.00$ 6,500.003/2/20213/1/20212/3/2022John Doe
27LKJ2147noyesUSAFJ3CSDUSAF-Package 776534Win XP$ 2,100.00$ 3,500.007/25/20217/5/20212/7/2022Jane Smith
28POI6698yesappleUSAJ4IODUSA-Package 3398Jwin xp$ 9,800.00$ 12,000.009/2/20214/16/20202/8/2022Jane Smith
29ABCS987nonoabcJ1SDDUSAF-Package 776534Windows 7$ 7,400.00$ 8,000.006/6/20213/1/20212/9/2022Joe Doe
30SMN3298noyesUSSFS4CSDUSSF-Package 2312Windows 7$ 6,500.00$ 9,500.009/22/20217/5/20212/10/2022Joe Doe
31LMN326yesappleabcS2CSDUSCG-Package 3287Windows XP$ 2,100.00$ 2,500.007/9/20213/1/20212/12/2022John Doe
32WDC589noUSNS6defUSN-Package 92374BBN Win 7 TTP77$ 1,750.00$ 8,700.002/1/202211/9/20202/13/2022Jane Doe
33VFD365yesyesUSNS6xyzUSN-Package 923Win xp$ 630.00$ 6,500.003/6/20213/25/20202/14/2022Jane Smith
34CVF127yesappleabcS3xyzUSMC-Package 789win 7$ 2,500.00$ 3,500.0012/1/20211/6/20212/15/2022Jane Smith
35CVRF9863yesnoUSNJ2SDDUSN-Package 923windows 7$ 7,800.00$ 7,800.002/2/20229/7/20202/16/2022Jane Smith
36EWD441yesyesUSAFS4CSDUSAF-Package 776534windows XP$ 1,200.00$ 4,500.008/5/20217/7/20202/18/2022John Doe
37RRB2285nonodefS5CSDUSAF-Package 776534Windows 7$ 3,200.00$ 4,500.009/5/20219/3/20202/19/2022John Doe
Sheet3
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Is it intended to duplicate the results when there's a match in both sets, since that rule is in both macros?
 
Upvote 0
Well, I'll create 3 more sheets for the results. Sheets 4, 5, and 6. So, anything with only Set 1 terms would go in Sheet 4, results with only Set 2 goes in Sheet 5, and if the results return keywords from both Sets 1 and 2, it would get pasted into Sheet 6.
 
Upvote 0
I am partially finished, but let me know if this is on the right track or not:

VBA Code:
Option Explicit

Sub SetFinder()

    Dim rngMain As Range, rngSht4 As Range
    Dim rngSht5 As Range, rngSht6 As Range
    Dim rngUnion As Range
    
    Dim i As Long, j As Long
    Dim setA As String, setB As String
    Dim bSetA As Boolean, bSetB As Boolean
    
    Set rngMain = Sheet3.Range("A2:N2")
    Set rngSht4 = Sheet4.Range("B2")
    Set rngSht5 = Sheet5.Range("B2")
    Set rngSht6 = Sheet6.Range("B2")

    setA = "abc, def, xyz"
    setB = "apple, banana, orange"
    
    For i = 0 To Sheet3.Range("A1048576").End(xlUp).Row - 2
        
        Set rngUnion = Nothing
        bSetA = False: bSetB = False
        For j = 0 To rngMain.Cells.Count - 1
        
            If InStr(1, setA, rngMain.Cells(i + 1, j + 1).Value) > 0 Then
                Set rngUnion = CheckRange(rngUnion, rngMain.Cells(i + 1, j + 1))
                bSetA = True
            End If
            
            If InStr(1, setB, rngMain.Cells(i + 1, j + 1).Value) > 0 Then
                Set rngUnion = CheckRange(rngUnion, rngMain.Cells(i + 1, j + 1))
                bSetB = True
            End If
            
        Next j
        
        If (bSetA And bSetB) Then
            
            If Not IsEmpty(rngSht6.Value) Then _
                Set rngSht6 = Sheet6.Range("B1048576").End(xlUp).Offset(1)
            rngSht6.Value = rngUnion.Value
            
        ElseIf bSetA Then
        
            If Not IsEmpty(rngSht4.Value) Then _
                Set rngSht4 = Sheet4.Range("B1048576").End(xlUp).Offset(1)
            rngSht4.Value = rngUnion.Value
        
        ElseIf bSetB Then
        
            If Not IsEmpty(rngSht5.Value) Then _
                Set rngSht5 = Sheet5.Range("B1048576").End(xlUp).Offset(1)
            rngSht5.Value = rngUnion.Value
        
        End If
        
    Next i

End Sub

Function CheckRange(rngUnion As Range, rngMain As Range) As Range
    
    Dim combRng As Range
    
    If rngUnion Is Nothing Then
        Set combRng = rngMain
    ElseIf Intersect(rngUnion, rngMain) Is Nothing Then
        Set combRng = Union(rngUnion, rngMain)
    Else
        Set combRng = rngUnion
    End If

    Set CheckRange = combRng
    
End Function
 
Upvote 0
One thing I need clarification on is whether the entire row needs to be pasted or just the cell containing the text of a set. Currently it's just pasting the matched cell's contents. I put the results for each Sheet 4 - 6 in cell B2 on down.
 
Upvote 0
One thing I need clarification on is whether the entire row needs to be pasted or just the cell containing the text of a set. Currently it's just pasting the matched cell's contents. I put the results for each Sheet 4 - 6 in cell B2 on down.
Sorry, I just saw this message. I would need the entire row pasted onto the new sheet.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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