+ Reply to Thread
Results 1 to 16 of 16

Excel VBA Code to find two words near each other (proximity search)

  1. #1
    Registered User
    Join Date
    01-13-2020
    Location
    CA
    MS-Off Ver
    2016
    Posts
    24

    Excel VBA Code to find two words near each other (proximity search)

    Hello -

    I am having a difficulty coming up with a macro that would do all of the following:
    1. Search a column of texts where two words are near each other regardless of order (e.g., within 5 words of each other)
    2. Highlight those cells
    3. Variations of the same word (e.g., charge, charges, charged, charging) needs to be taken into account

    I am relatively new to using VBA, so I appreciate any help or insight!

    Example: I want to find and highlight the cells in a column where the word "incorrect" is within 3 words of "bill"
    1. The vendor billed the company incorrectly. -> this cell would be highlighted
    2. The vendor sent an incorrect bill to the company. -> this cell would be highlighted
    3. The vendor did not send a bill to the company. -> this cell would not be highlighted

    I was thinking to use Regular Expression. Any thoughts?
    Last edited by demiwen12; 01-13-2020 at 09:54 PM.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,914

    Re: Excel VBA Code to find two words near each other (proximity search)

    Try this macro. I'm assuming your data is in column A. Change the columns letters (in red) to suit your needs.
    Please Login or Register  to view this content.
    Last edited by Mumps1; 01-13-2020 at 05:32 PM.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,159

    Re: Excel VBA Code to find two words near each other (proximity search)

    Edit:

    Do not pay attention, mistake ..
    Last edited by mjr veverka; 01-13-2020 at 05:26 PM.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,710

    Re: Excel VBA Code to find two words near each other (proximity search)

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-13-2020
    Location
    CA
    MS-Off Ver
    2016
    Posts
    24

    Re: Excel VBA Code to find two words near each other (proximity search)

    Quote Originally Posted by jindon View Post
    Please Login or Register  to view this content.

    This is perfect! Thanks so much. One follow-up question, if I want to call this code using function within a cell to create a summary count of these hits in a different column, how should I modify the code? So with my example below:

    1. The vendor billed the company incorrectly.
    2. The vendor sent an incorrect bill to the company.
    3. The vendor did not send a bill to the company.

    My summary count would look like:

    Cell A1 (text): "incorrect" within 3 words of "bill" -> this is manual input
    Cell B1 (summary count): 2 -> function to call the code that tallies the total hits

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,710

    Re: Excel VBA Code to find two words near each other (proximity search)

    Quote Originally Posted by demiwen12 View Post

    Cell A1 (text): "The vendor billed the company incorrectly."
    Cell B1 (summary count): 2 -> function to call the code that tallies the total hits
    B1:
    =SummaryCount(A1)
    Please Login or Register  to view this content.
    Last edited by jindon; 01-14-2020 at 10:07 PM.

  7. #7
    Registered User
    Join Date
    01-13-2020
    Location
    CA
    MS-Off Ver
    2016
    Posts
    24

    Re: Excel VBA Code to find two words near each other (proximity search)

    Because based on the criteria - "incorrect" within 3 words of "bill" - only #1 and #2 below are positive hits. Therefore, the count should be 2. Hope that makes sense!

    1. The vendor billed the company incorrectly.
    2. The vendor sent an incorrect bill to the company.
    3. The vendor did not send a bill to the company.

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,710

    Re: Excel VBA Code to find two words near each other (proximity search)

    Yep, I have just edited my code, so try it.

  9. #9
    Registered User
    Join Date
    01-13-2020
    Location
    CA
    MS-Off Ver
    2016
    Posts
    24

    Re: Excel VBA Code to find two words near each other (proximity search)

    Quote Originally Posted by jindon View Post
    B1:
    =SummaryCount(A1)
    Please Login or Register  to view this content.

    Thanks! However, I should probably be clearer in my previous post. Please see my attached test workbook. Note that Column A in the "Source" worksheet may be much longer and I need to capture the count through all of them.
    Attached Files Attached Files

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,710

    Re: Excel VBA Code to find two words near each other (proximity search)

    1) Do not quote full post, unless you really need it to. The forum doesn't like it.

    2)
    B2:
    =SummaryCount(Source!A1:A6,A2)
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    01-13-2020
    Location
    CA
    MS-Off Ver
    2016
    Posts
    24

    Re: Excel VBA Code to find two words near each other (proximity search)

    Thanks! I tried my best to modify the code a little bit, but it gave me the #VALUE error . Can you take a look at it? I'm trying to count the unique rows of these matches, regardless of how many matches are within a cell (i.e., if in cell A1 there are 2 matches of incorrect within 3 words of bill, I only want 1 to return since only 1 unique row is involved). I attached the workbook again.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,710

    Re: Excel VBA Code to find two words near each other (proximity search)

    Don't try to let users generate pattern for regular expressions, it is not that simple/easy.

    Is this the final format for criteria?
    I don't want to do it every time you change your mind.

  13. #13
    Registered User
    Join Date
    01-13-2020
    Location
    CA
    MS-Off Ver
    2016
    Posts
    24

    Re: Excel VBA Code to find two words near each other (proximity search)

    Basically I have a set of search terms that may be in a different format from the one I provided. My list, which has about 15 terms, is finalized, so it is unlikely that there will be additional terms. Examples of some of my terms look like the below; hence, the reason why I tried to edit the code the way I did since the pattern is not consistent and can be quite complex. My plan was to write out these terms in regular expressions, paste them in the "Pattern" column, and then just reference this column when I count the matches (referring to the workbook previously attached).

    - incorrect* w/3 bill*
    - (reclass* OR charg* OR edit*) w/5 (time OR hour*)
    - actual w/3 (time OR hour*)
    - budget*

    Sorry about the back and forth!

    [Edit] Also just want to note that I've got the pattern written out for all my terms in regular expressions and when I run the searches, they do work. Just need to figure out count!
    Last edited by demiwen12; 01-16-2020 at 01:09 AM.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,710

    Re: Excel VBA Code to find two words near each other (proximity search)

    Quote Originally Posted by demiwen12 View Post

    [Edit] Also just want to note that I've got the pattern written out for all my terms in regular expressions and when I run the searches, they do work. Just need to figure out count!
    Are you sure?
    When you use asterisk * or other meta-characters in the pattern, you will need to escape them, if you do a LIKE comparison, existence of pre/suf fix will also be the matter.

    Anyway, if you know how to create the pattern, you don't need any help...

    BTW, don't place "ON ERROR" statement in the first line, you will not find the cause of the error...

    P.S
    Quote Originally Posted by demiwen12 View Post
    Please Login or Register  to view this content.
    There is no Rows.Count, just Count, and your function will return 1 or 0, nothing else...
    Last edited by jindon; 01-16-2020 at 06:37 AM.

  15. #15
    Registered User
    Join Date
    01-13-2020
    Location
    CA
    MS-Off Ver
    2016
    Posts
    24

    Re: Excel VBA Code to find two words near each other (proximity search)

    Thanks a million and for the feedback! The asterisk are only for presentation purposes, showing the audience what kind of terms were searched, etc. In the code, I simply followed the pattern provided in your first post for all my terms, and they work miracle.

    Quote Originally Posted by jindon View Post
    Please Login or Register  to view this content.
    Been reading up on creating patterns in regex too and yes I agree they are pretty complicated.. But so far I've got what I need! Appreciate your time and help

  16. #16
    Registered User
    Join Date
    02-19-2021
    Location
    Richmond VA
    MS-Off Ver
    O365
    Posts
    1

    Re: Excel VBA Code to find two words near each other (proximity search)

    Great posts - really helped me out. I took the version here and added some user prompts for columns to search, terms to use, proximity distance. it is great!

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 18
    Last Post: 05-15-2018, 05:17 AM
  2. [SOLVED] Vba code to search key words in descriptions column
    By xlhelp7 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-18-2017, 02:37 AM
  3. [SOLVED] Macro to search for specific words in proximity to each other?
    By BPSJACK in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-25-2014, 11:55 AM
  4. Setting up a search macro to find multiple words
    By O0J0E in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-19-2013, 05:14 AM
  5. Want an VB code to search / replace words in another macro
    By CatEnters in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-08-2012, 07:49 PM
  6. [SOLVED] Search Cells for key words and add category code
    By Jimbo42 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-07-2012, 05:23 PM
  7. Replies: 1
    Last Post: 04-04-2012, 07:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1