+ Reply to Thread
Results 1 to 5 of 5

Need help modifying VBA function to search only complete words.

  1. #1
    Registered User
    Join Date
    03-18-2014
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    3

    Need help modifying VBA function to search only complete words.

    Hey guys,

    I've been working on a method to search a single column of 1000+ cells containing a paragraph with a 250+ word keyword list (also in a single column). I'm needing only complete words to be searched/ displayed regardless of spacing & punctuation. I've tried multiple methods the past few days and the one that worked (Macro with 4 complex formulas) was unusable when scaled out to all 1,000 rows due to the extremely long calculating time. I don't know VBA yet but was able to modify and create a user defined function that does everything I need except it displays string results that are not whole words (Ex. search for air, word in cell is fair, displays a result for air).

    If anyone can help me add conditions to my code that search and return only complete words I'd greatly appreciate it.


    PHP Code: 
    Function RangeSearch2(text As Stringwordlist As Rangeseperator As StringOptional caseSensitive As Boolean False)
        
    Dim strMatches As String
        Dim res 
    As Variant
        Dim arrWords
    () As String
        Dim skip 
    As Boolean
        skip 
    False
        On Error Resume Next
        Err
    .Clear
        
    For Each word In wordlist
                
    If caseSensitive False Then
                    res 
    InStr(LCase(text), LCase(word))
                Else
                    
    res InStr(textword)
                
    End If
                If 
    res 0 Then
                    strMatches 
    strMatches seperator word
                End 
    If
        
    Next word
         
    If Len(strMatches) <> 0 Then
            strMatches 
    Right(strMatchesLen(strMatches) - Len(seperator))
        
    End If
        
    RangeSearch strMatches
    End 
    Function 
    Last edited by Invisible Hand; 03-19-2014 at 10:59 PM.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Need help modifying VBA function to search only complete words.

    Hi, Invisible Hand,

    InStr will always return any partial match as it will check if that part is anywhere in the searched string so that UDF should turn out not to be what you want. IŽd guess you would have to use Find/FindNext and xlWhole for a match.

    CIao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    03-18-2014
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Need help modifying VBA function to search only complete words.

    Quote Originally Posted by HaHoBe View Post
    Hi, Invisible Hand,

    InStr will always return any partial match as it will check if that part is anywhere in the searched string so that UDF should turn out not to be what you want. IŽd guess you would have to use Find/FindNext and xlWhole for a match.

    CIao,
    Holger
    Crap. I guess I wasted another 2 hours on this.

    I appreciate the heads up though. I wont waste any more time trying to embed the UDF into an IF function to filer out unwanted results LOL. I wonder how quick the paid help on here is.

    Thanks for the reply,

    -Jared

  4. #4
    Registered User
    Join Date
    03-18-2014
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Need help modifying VBA function to search only complete words.

    Got it working by altering the word list portion.

    Now all I need is macro that will do the following:
    -run from a button that is copied to 1000+ rows
    -highlight individual words in found a cell that match words in another cell on the same row
    -uses the location of the button to identify which cells to be searched and referenced (since the button will be used on multiple rows)
    -opens the now searched and highlighted cell as if being double clicked to display its entire contents.

    Any advice as to where to start would be appreciated. I've been playing around with different methods for a while now.

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Need help modifying VBA function to search only complete words.

    Hi, Invisible Hand,

    maybe use either Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) or Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean) behind the sheet to trigger the search, Both will deliver the row number as Target.Row and could be used instead of copying buttons. Narrow the area where to work down by chekcing via Intersect and use Cancel = True to disable the normal behaviour for the event.

    Ciao,
    Holger

+ 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. [SOLVED] Date Function for Auto complete Textbox Search
    By paralegal91 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-14-2014, 03:49 PM
  2. Replies: 1
    Last Post: 04-04-2012, 07:15 PM
  3. Matching Whole Words Only in a Lookup / Search Function
    By cquest in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-25-2011, 10:48 AM
  4. Modifying a sumproduct formula to search for words in a cell
    By Weasel in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-07-2009, 02:51 PM
  5. search function for specific words
    By ducky77ar in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-25-2007, 10:17 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