Excel VBA Like statement with singular/plural words
I'm trying to make a Document Search Engine that uses keywords and the document name when searching for a file.. in an excel spreadsheet.
It works by breaking down the search term and the name of the file via spaces, as well as the keywords via commas.
Then it matches each search term for every keyword, and adds a counter for each match. More keyword matches = higher counter, then I sort the table via this counter, so I could get the best-matching result at the top.
The search functionality is already working fine, however when it comes to singular and plural terms, it's not finding it.
Let's say that the keyword for doc1 is Templates. When searching for the word Template in the search field, it fails to match. I'm using a LIKE statement, and I tried adding a wildcard at the front and the end, but it still does not work.
Strangely enough, the opposite works. If I search for a plural term, and the keyword is singular, it gets a match. I have no idea why, it just works which is a good thing.
Sub Searchresult()
Dim x As Long, y As Long, count As Long
Dim search() As String, keyword() As String, names() As String
Dim result As String
Dim tbl As ListObject, sortcol As Range, lrow As Long
With Worksheets("Sheet3") 'Prep for placing results in table.
Set tbl = .ListObjects("tblSearch")
Set sortcol = .Range("tblSearch[sort]")
tbl.DataBodyRange.ClearContents
End With
With Worksheets("Sheet2")
search = Split(.Range("F1").Value, " ") 'split search terms via spaces
For x = 2 To 1000 Step 1
count = 0
lrow = Worksheets("Sheet3").Cells(Rows.count, 1).End(xlUp).Row + 1
keyword() = Split(.Range("d" & x), ",") ' split keywords via comma
names() = Split(Replace(Replace(Replace(Replace(.Range("c" & x), "-", ""), "(", ""), ")", ""), "'", ""), " ") 'splits names via spaces, deleting any unwanted characters
For i = LBound(keyword) To UBound(keyword)
For j = LBound(search) To UBound(search)
If "*" & UCase(search(j)) & "*" Like "*" & UCase(keyword(i)) & "*" Then 'compare search term and keyword
Worksheets("Sheet3").Range("A" & lrow, "B" & lrow).Value = .Range("A" & x, "B" & x).Value
count = count + 1
Worksheets("Sheet3").Range("C" & lrow).Value = count
End If
Next
Next
For k = LBound(names) To UBound(names)
For l = LBound(search) To UBound(search)
If "*" & UCase(search(l)) & "*" Like "*" & UCase(names(k)) & "*" And Len(names(k)) > 2 Then 'compare search term and document name
Worksheets("Sheet3").Range("A" & lrow, "B" & lrow).Value = .Range("A" & x, "B" & x).Value
count = count + 1
Worksheets("Sheet3").Range("C" & lrow).Value = count
End If
Next
Next
Next
End With
With tbl.Sort 'sort everything based on count to get best result
.SortFields.Clear
.SortFields.Add Key:=sortcol, SortOn:=xlSortOnValues, Order:=xlDescending
.Header = xlYes
.Apply
End With
End Sub
How do I make sure that the LIKE statement can match both singular/plural terms of a word? It doesn't have to be too complex that it can find words that change when it's singular/plural (Ex:"copy/copies"), however having it match something that just adds an 's' would do.
EDIT: Thanks to Brian's explanation, the code is fixed by adding an OR statement that flips the Like statement so that it compares the terms again. It's now able to match even though it's singular/plural.
If UCase(search(j)) Like "*" & UCase(keyword(i)) & "*" Or UCase(keyword(i)) Like UCase(search(j)) & "*" Then 'compare search term and keyword
If (UCase(search(l)) & "*" Like UCase(names(k)) & "*" Or UCase(names(k)) Like UCase(search(l)) & "*") And Len(names(k)) > 2 Then
excel vba search
add a comment |
I'm trying to make a Document Search Engine that uses keywords and the document name when searching for a file.. in an excel spreadsheet.
It works by breaking down the search term and the name of the file via spaces, as well as the keywords via commas.
Then it matches each search term for every keyword, and adds a counter for each match. More keyword matches = higher counter, then I sort the table via this counter, so I could get the best-matching result at the top.
The search functionality is already working fine, however when it comes to singular and plural terms, it's not finding it.
Let's say that the keyword for doc1 is Templates. When searching for the word Template in the search field, it fails to match. I'm using a LIKE statement, and I tried adding a wildcard at the front and the end, but it still does not work.
Strangely enough, the opposite works. If I search for a plural term, and the keyword is singular, it gets a match. I have no idea why, it just works which is a good thing.
Sub Searchresult()
Dim x As Long, y As Long, count As Long
Dim search() As String, keyword() As String, names() As String
Dim result As String
Dim tbl As ListObject, sortcol As Range, lrow As Long
With Worksheets("Sheet3") 'Prep for placing results in table.
Set tbl = .ListObjects("tblSearch")
Set sortcol = .Range("tblSearch[sort]")
tbl.DataBodyRange.ClearContents
End With
With Worksheets("Sheet2")
search = Split(.Range("F1").Value, " ") 'split search terms via spaces
For x = 2 To 1000 Step 1
count = 0
lrow = Worksheets("Sheet3").Cells(Rows.count, 1).End(xlUp).Row + 1
keyword() = Split(.Range("d" & x), ",") ' split keywords via comma
names() = Split(Replace(Replace(Replace(Replace(.Range("c" & x), "-", ""), "(", ""), ")", ""), "'", ""), " ") 'splits names via spaces, deleting any unwanted characters
For i = LBound(keyword) To UBound(keyword)
For j = LBound(search) To UBound(search)
If "*" & UCase(search(j)) & "*" Like "*" & UCase(keyword(i)) & "*" Then 'compare search term and keyword
Worksheets("Sheet3").Range("A" & lrow, "B" & lrow).Value = .Range("A" & x, "B" & x).Value
count = count + 1
Worksheets("Sheet3").Range("C" & lrow).Value = count
End If
Next
Next
For k = LBound(names) To UBound(names)
For l = LBound(search) To UBound(search)
If "*" & UCase(search(l)) & "*" Like "*" & UCase(names(k)) & "*" And Len(names(k)) > 2 Then 'compare search term and document name
Worksheets("Sheet3").Range("A" & lrow, "B" & lrow).Value = .Range("A" & x, "B" & x).Value
count = count + 1
Worksheets("Sheet3").Range("C" & lrow).Value = count
End If
Next
Next
Next
End With
With tbl.Sort 'sort everything based on count to get best result
.SortFields.Clear
.SortFields.Add Key:=sortcol, SortOn:=xlSortOnValues, Order:=xlDescending
.Header = xlYes
.Apply
End With
End Sub
How do I make sure that the LIKE statement can match both singular/plural terms of a word? It doesn't have to be too complex that it can find words that change when it's singular/plural (Ex:"copy/copies"), however having it match something that just adds an 's' would do.
EDIT: Thanks to Brian's explanation, the code is fixed by adding an OR statement that flips the Like statement so that it compares the terms again. It's now able to match even though it's singular/plural.
If UCase(search(j)) Like "*" & UCase(keyword(i)) & "*" Or UCase(keyword(i)) Like UCase(search(j)) & "*" Then 'compare search term and keyword
If (UCase(search(l)) & "*" Like UCase(names(k)) & "*" Or UCase(names(k)) Like UCase(search(l)) & "*") And Len(names(k)) > 2 Then
excel vba search
add a comment |
I'm trying to make a Document Search Engine that uses keywords and the document name when searching for a file.. in an excel spreadsheet.
It works by breaking down the search term and the name of the file via spaces, as well as the keywords via commas.
Then it matches each search term for every keyword, and adds a counter for each match. More keyword matches = higher counter, then I sort the table via this counter, so I could get the best-matching result at the top.
The search functionality is already working fine, however when it comes to singular and plural terms, it's not finding it.
Let's say that the keyword for doc1 is Templates. When searching for the word Template in the search field, it fails to match. I'm using a LIKE statement, and I tried adding a wildcard at the front and the end, but it still does not work.
Strangely enough, the opposite works. If I search for a plural term, and the keyword is singular, it gets a match. I have no idea why, it just works which is a good thing.
Sub Searchresult()
Dim x As Long, y As Long, count As Long
Dim search() As String, keyword() As String, names() As String
Dim result As String
Dim tbl As ListObject, sortcol As Range, lrow As Long
With Worksheets("Sheet3") 'Prep for placing results in table.
Set tbl = .ListObjects("tblSearch")
Set sortcol = .Range("tblSearch[sort]")
tbl.DataBodyRange.ClearContents
End With
With Worksheets("Sheet2")
search = Split(.Range("F1").Value, " ") 'split search terms via spaces
For x = 2 To 1000 Step 1
count = 0
lrow = Worksheets("Sheet3").Cells(Rows.count, 1).End(xlUp).Row + 1
keyword() = Split(.Range("d" & x), ",") ' split keywords via comma
names() = Split(Replace(Replace(Replace(Replace(.Range("c" & x), "-", ""), "(", ""), ")", ""), "'", ""), " ") 'splits names via spaces, deleting any unwanted characters
For i = LBound(keyword) To UBound(keyword)
For j = LBound(search) To UBound(search)
If "*" & UCase(search(j)) & "*" Like "*" & UCase(keyword(i)) & "*" Then 'compare search term and keyword
Worksheets("Sheet3").Range("A" & lrow, "B" & lrow).Value = .Range("A" & x, "B" & x).Value
count = count + 1
Worksheets("Sheet3").Range("C" & lrow).Value = count
End If
Next
Next
For k = LBound(names) To UBound(names)
For l = LBound(search) To UBound(search)
If "*" & UCase(search(l)) & "*" Like "*" & UCase(names(k)) & "*" And Len(names(k)) > 2 Then 'compare search term and document name
Worksheets("Sheet3").Range("A" & lrow, "B" & lrow).Value = .Range("A" & x, "B" & x).Value
count = count + 1
Worksheets("Sheet3").Range("C" & lrow).Value = count
End If
Next
Next
Next
End With
With tbl.Sort 'sort everything based on count to get best result
.SortFields.Clear
.SortFields.Add Key:=sortcol, SortOn:=xlSortOnValues, Order:=xlDescending
.Header = xlYes
.Apply
End With
End Sub
How do I make sure that the LIKE statement can match both singular/plural terms of a word? It doesn't have to be too complex that it can find words that change when it's singular/plural (Ex:"copy/copies"), however having it match something that just adds an 's' would do.
EDIT: Thanks to Brian's explanation, the code is fixed by adding an OR statement that flips the Like statement so that it compares the terms again. It's now able to match even though it's singular/plural.
If UCase(search(j)) Like "*" & UCase(keyword(i)) & "*" Or UCase(keyword(i)) Like UCase(search(j)) & "*" Then 'compare search term and keyword
If (UCase(search(l)) & "*" Like UCase(names(k)) & "*" Or UCase(names(k)) Like UCase(search(l)) & "*") And Len(names(k)) > 2 Then
excel vba search
I'm trying to make a Document Search Engine that uses keywords and the document name when searching for a file.. in an excel spreadsheet.
It works by breaking down the search term and the name of the file via spaces, as well as the keywords via commas.
Then it matches each search term for every keyword, and adds a counter for each match. More keyword matches = higher counter, then I sort the table via this counter, so I could get the best-matching result at the top.
The search functionality is already working fine, however when it comes to singular and plural terms, it's not finding it.
Let's say that the keyword for doc1 is Templates. When searching for the word Template in the search field, it fails to match. I'm using a LIKE statement, and I tried adding a wildcard at the front and the end, but it still does not work.
Strangely enough, the opposite works. If I search for a plural term, and the keyword is singular, it gets a match. I have no idea why, it just works which is a good thing.
Sub Searchresult()
Dim x As Long, y As Long, count As Long
Dim search() As String, keyword() As String, names() As String
Dim result As String
Dim tbl As ListObject, sortcol As Range, lrow As Long
With Worksheets("Sheet3") 'Prep for placing results in table.
Set tbl = .ListObjects("tblSearch")
Set sortcol = .Range("tblSearch[sort]")
tbl.DataBodyRange.ClearContents
End With
With Worksheets("Sheet2")
search = Split(.Range("F1").Value, " ") 'split search terms via spaces
For x = 2 To 1000 Step 1
count = 0
lrow = Worksheets("Sheet3").Cells(Rows.count, 1).End(xlUp).Row + 1
keyword() = Split(.Range("d" & x), ",") ' split keywords via comma
names() = Split(Replace(Replace(Replace(Replace(.Range("c" & x), "-", ""), "(", ""), ")", ""), "'", ""), " ") 'splits names via spaces, deleting any unwanted characters
For i = LBound(keyword) To UBound(keyword)
For j = LBound(search) To UBound(search)
If "*" & UCase(search(j)) & "*" Like "*" & UCase(keyword(i)) & "*" Then 'compare search term and keyword
Worksheets("Sheet3").Range("A" & lrow, "B" & lrow).Value = .Range("A" & x, "B" & x).Value
count = count + 1
Worksheets("Sheet3").Range("C" & lrow).Value = count
End If
Next
Next
For k = LBound(names) To UBound(names)
For l = LBound(search) To UBound(search)
If "*" & UCase(search(l)) & "*" Like "*" & UCase(names(k)) & "*" And Len(names(k)) > 2 Then 'compare search term and document name
Worksheets("Sheet3").Range("A" & lrow, "B" & lrow).Value = .Range("A" & x, "B" & x).Value
count = count + 1
Worksheets("Sheet3").Range("C" & lrow).Value = count
End If
Next
Next
Next
End With
With tbl.Sort 'sort everything based on count to get best result
.SortFields.Clear
.SortFields.Add Key:=sortcol, SortOn:=xlSortOnValues, Order:=xlDescending
.Header = xlYes
.Apply
End With
End Sub
How do I make sure that the LIKE statement can match both singular/plural terms of a word? It doesn't have to be too complex that it can find words that change when it's singular/plural (Ex:"copy/copies"), however having it match something that just adds an 's' would do.
EDIT: Thanks to Brian's explanation, the code is fixed by adding an OR statement that flips the Like statement so that it compares the terms again. It's now able to match even though it's singular/plural.
If UCase(search(j)) Like "*" & UCase(keyword(i)) & "*" Or UCase(keyword(i)) Like UCase(search(j)) & "*" Then 'compare search term and keyword
If (UCase(search(l)) & "*" Like UCase(names(k)) & "*" Or UCase(names(k)) Like UCase(search(l)) & "*") And Len(names(k)) > 2 Then
excel vba search
excel vba search
edited Nov 13 '18 at 20:16
Basher
asked Nov 13 '18 at 18:06
BasherBasher
25718
25718
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
In your example code, the 'keyword' needs to be singular and then it will match both singular and plural (as you have seen). Also, you don't need wildcards on the 'search' term.
"TEMPLATE" Like "*" & "TEMPLATE" & "*" 'returns True
"TEMPLATES" Like "*" & "TEMPLATE" & "*" 'returns True
Think of it this way. In both the above cases, we are in essence saying "does the first term CONTAIN the second term.
That makes a lot of sense, thanks. I thought the LIKE operator logic is to check if they are similar, not a contains b. Love this community, been learning a lot. I've added an OR statement that just switches the two terms, and it looks like it's working.
– Basher
Nov 13 '18 at 18:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287051%2fexcel-vba-like-statement-with-singular-plural-words%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your example code, the 'keyword' needs to be singular and then it will match both singular and plural (as you have seen). Also, you don't need wildcards on the 'search' term.
"TEMPLATE" Like "*" & "TEMPLATE" & "*" 'returns True
"TEMPLATES" Like "*" & "TEMPLATE" & "*" 'returns True
Think of it this way. In both the above cases, we are in essence saying "does the first term CONTAIN the second term.
That makes a lot of sense, thanks. I thought the LIKE operator logic is to check if they are similar, not a contains b. Love this community, been learning a lot. I've added an OR statement that just switches the two terms, and it looks like it's working.
– Basher
Nov 13 '18 at 18:56
add a comment |
In your example code, the 'keyword' needs to be singular and then it will match both singular and plural (as you have seen). Also, you don't need wildcards on the 'search' term.
"TEMPLATE" Like "*" & "TEMPLATE" & "*" 'returns True
"TEMPLATES" Like "*" & "TEMPLATE" & "*" 'returns True
Think of it this way. In both the above cases, we are in essence saying "does the first term CONTAIN the second term.
That makes a lot of sense, thanks. I thought the LIKE operator logic is to check if they are similar, not a contains b. Love this community, been learning a lot. I've added an OR statement that just switches the two terms, and it looks like it's working.
– Basher
Nov 13 '18 at 18:56
add a comment |
In your example code, the 'keyword' needs to be singular and then it will match both singular and plural (as you have seen). Also, you don't need wildcards on the 'search' term.
"TEMPLATE" Like "*" & "TEMPLATE" & "*" 'returns True
"TEMPLATES" Like "*" & "TEMPLATE" & "*" 'returns True
Think of it this way. In both the above cases, we are in essence saying "does the first term CONTAIN the second term.
In your example code, the 'keyword' needs to be singular and then it will match both singular and plural (as you have seen). Also, you don't need wildcards on the 'search' term.
"TEMPLATE" Like "*" & "TEMPLATE" & "*" 'returns True
"TEMPLATES" Like "*" & "TEMPLATE" & "*" 'returns True
Think of it this way. In both the above cases, we are in essence saying "does the first term CONTAIN the second term.
answered Nov 13 '18 at 18:30
Brian M StaffordBrian M Stafford
2,9491914
2,9491914
That makes a lot of sense, thanks. I thought the LIKE operator logic is to check if they are similar, not a contains b. Love this community, been learning a lot. I've added an OR statement that just switches the two terms, and it looks like it's working.
– Basher
Nov 13 '18 at 18:56
add a comment |
That makes a lot of sense, thanks. I thought the LIKE operator logic is to check if they are similar, not a contains b. Love this community, been learning a lot. I've added an OR statement that just switches the two terms, and it looks like it's working.
– Basher
Nov 13 '18 at 18:56
That makes a lot of sense, thanks. I thought the LIKE operator logic is to check if they are similar, not a contains b. Love this community, been learning a lot. I've added an OR statement that just switches the two terms, and it looks like it's working.
– Basher
Nov 13 '18 at 18:56
That makes a lot of sense, thanks. I thought the LIKE operator logic is to check if they are similar, not a contains b. Love this community, been learning a lot. I've added an OR statement that just switches the two terms, and it looks like it's working.
– Basher
Nov 13 '18 at 18:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287051%2fexcel-vba-like-statement-with-singular-plural-words%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown