Compare and extract the words that matches

ExcelNewbie2020

Active Member
Joined
Dec 3, 2020
Messages
293
Office Version
  1. 365
Platform
  1. Windows
sirs,

i need to compare the words in "Particular" column in sheet 1 against the "Particular" column of sheet 2.
Then, matched words will be extracted accordingly..
Comparison must be based with "Category". Please see below table for details

Name-List-for-Practice.xlsm
ABCDE
1SHEET1SHEET 2
2CategoryParticularsExpected resultParticularsCategory
3asmall portal hub editors method resources taskssmall, method, small, methodCommunity portal – The central hub for editors, with resources, links, tasks, and announcements.d
4anote method works best small descriptionsnote, method, works, best, small, descriptions, note, method, works, best, small, descriptionsVillage pump – Forum for discussions about Wikipedia itself, including policies and technical issues.d
5anews Sources of news about broadernews, broaderSite news – Sources of news about Wikipedia and the broader Wikimedia movement.d
6bTeahouse Ask basic questions editing Wikipedia.questionsTeahouse Ask basic questions about using or editing Wikipedia.c
7bHelp desk about using editing Wikipedia.desk, aboutHelp desk Ask questions about using or editing Wikipedia.c
8bReference desk Ask research questions encyclopedicReference, research, questions, encyclopedicReference desk Ask research questions about encyclopedic topics.b
9chelp Content unique teahouse way to navigate encyclopediaTeahouse, helpContent portals – A unique way to navigate the encyclopedia.b
10Please note that this method works best for small descriptionsa
11Please note that this method works best for small descriptionsa
12news portal – sources central hub for editors with news broader tasks and announcements.a
13
Sheet2
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I'm not sure that I totally follow your logic. For example, in the first row you list 'small' and 'method' twice each (because they appear twice in the "a" 'Category' on sheet 2) - but you don't include 'portal', 'hub', 'editors' or 'tasks' which also appear in the "a" 'Category' on sheet 2? In row 6 you expect 'questions' but you don't expect 'Ask' which also appears in the "b" 'Category' on sheet 2?
Anyhow, the following code is a starting point which will no doubt be improved (by me or others) as we go along. Starting with the following layouts on sheets 1 & 2:
test.xlsm
ABC
1CategoryParticularsExpected result
2asmall portal hub editors method resources tasks
3anote method works best small descriptions
4anews Sources of news about broader
5bTeahouse Ask basic questions editing Wikipedia.
6bHelp desk about using editing Wikipedia.
7bReference desk Ask research questions encyclopedic
8chelp Content unique teahouse way to navigate encyclopedia
Sheet1

test.xlsm
AB
1ParticularsCategory
2Community portal – The central hub for editors, with resources, links, tasks, and announcements.d
3Village pump – Forum for discussions about Wikipedia itself, including policies and technical issues.d
4Site news – Sources of news about Wikipedia and the broader Wikimedia movement.d
5Teahouse Ask basic questions about using or editing Wikipedia.c
6Help desk Ask questions about using or editing Wikipedia.c
7Reference desk Ask research questions about encyclopedic topics.b
8Content portals – A unique way to navigate the encyclopedia.b
9Please note that this method works best for small descriptionsa
10Please note that this method works best for small descriptionsa
11news portal – sources central hub for editors with news broader tasks and announcements.a
Sheet2


The following code...
VBA Code:
Option Explicit
Sub Test_Compare()
    Dim ws1 As Worksheet, ws2 As Worksheet, r As Range
    Set ws1 = Worksheets("Sheet1")          '<-- *** Change to actual sheet names ***
    Set ws2 = Worksheets("Sheet2")
    Dim a, b, c, d
    Dim i As Long, j As Long, k As Long
    Dim s1 As String, s2 As String, s3 As String
    
    Set r = ws1.Range("A2", ws1.Cells(Rows.Count, "B").End(xlUp))
    r.Value2 = Application.Trim(r)
    a = r
    b = ws2.Range("A2", ws2.Cells(Rows.Count, "B").End(xlUp))
    ReDim d(1 To UBound(a, 1), 1 To 1)
    
    For i = 1 To UBound(a, 1)
        s1 = a(i, 1)
        For j = 1 To UBound(b, 1)
            If b(j, 2) = s1 Then s2 = s2 & " " & b(j, 1)
        Next j
        c = Split(Trim(a(i, 2)))
        For k = LBound(c) To UBound(c)
            If InStr(1, s2, c(k), vbTextCompare) > 0 Then s3 = s3 & c(k) & ", "
        Next k
        s3 = Left(s3, Len(s3) - 2): d(i, 1) = s3: s3 = ""
    Next i

ws1.Range("c2").Resize(UBound(d, 1), 1).Value = d
End Sub

Produces this result...
test.xlsm
ABC
1CategoryParticularsExpected result
2asmall portal hub editors method resources taskssmall, portal, hub, editors, method, tasks
3anote method works best small descriptionsnote, method, works, best, small, descriptions
4anews Sources of news about broadernews, Sources, news, broader
5bTeahouse Ask basic questions editing Wikipedia.Ask, questions
6bHelp desk about using editing Wikipedia.desk, about
7bReference desk Ask research questions encyclopedicReference, desk, Ask, research, questions, encyclopedic
8chelp Content unique teahouse way to navigate encyclopediahelp, Content, unique, teahouse, way, to, navigate, encyclopedia
Sheet1
 
Upvote 0
I'm not sure that I totally follow your logic. For example, in the first row you list 'small' and 'method' twice each (because they appear twice in the "a" 'Category' on sheet 2) - but you don't include 'portal', 'hub', 'editors' or 'tasks' which also appear in the "a" 'Category' on sheet 2? In row 6 you expect 'questions' but you don't expect 'Ask' which also appears in the "b" 'Category' on sheet 2?
Anyhow, the following code is a starting point which will no doubt be improved (by me or others) as we go along. Starting with the following layouts on sheets 1 & 2:
my bad..your are right.. i had missed those words.. the code works fine with category a and b except for category c.

thank you sir for the reply. really appreciate..
 
Upvote 0
except for category c.
When I look at category c on sheet 1, and compare it with category c on sheet 2, it seems to me to be working correctly. What did you expect category c to return in the Expected result column (given that all the words that are being returned in the demo in post #2 are present in the category c section of sheet 2)?
 
Upvote 0

Forum statistics

Threads
1,215,301
Messages
6,124,146
Members
449,144
Latest member
Rayudo125

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