Consulting

Results 1 to 4 of 4

Thread: Find Specific Word from the cell by referring a list (VBA)

  1. #1
    VBAX Regular
    Joined
    Feb 2012
    Posts
    46
    Location

    Exclamation Find Specific Word from the cell by referring a list (VBA)

    HI Support,

    I've a excel workbook with two spreadsheet.

    The first sheet(Data) contains list of data. i want to find out if it matches with any specific word from sheet2 (Words to Find) and print the word in Column B.

    If there no match found against sheet2(Words to find) , then for that cell, the result should be "No match found". Can you provide a VBA code for this.

    I have attached a sample workbook for this. Thank you in Advance

    For Example:

    Sheet1Data)

    1. The cat stretched
    2. Jacob stood on his tiptoes
    3. The cat Stretched
    4. The car turned the corner
    5. Kelly twirled in circles
    6. She opened the door
    7. Aaron made a picture
    8. I'm so sorry
    9. I danced like any thing




    Sheet 2Words to Find)
    The Cat
    The car
    Jacob
    in Circles
    so sorry
    the Door
    a Picture
    Stretched
    Any thing
    Attached Files Attached Files
    Last edited by sethu29; 04-17-2021 at 01:53 PM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Option Explicit
    
    
    Sub SearchWords()
        Dim rCell As Range, rFind As Range
        Dim vFind As Variant, vTemp As Variant
        Dim i As Long, iFound As Long
        Dim sFind As String, sSearch As String
        
        'if range not selected then get out
        If Not TypeOf Selection Is Range Then Exit Sub
    
    
        'build UC string of words to find separated by #
        Set rFind = Worksheets("Words to find").Cells(1, 1).CurrentRegion
        Set rFind = rFind(2, 1).Resize(rFind.Rows.Count - 1, 1)
        sFind = UCase(Join(Application.WorksheetFunction.Transpose(rFind), "#"))
        
        'check selection for each data word
        For Each rCell In Selection.Columns(1).Cells
                
            sSearch = rCell.Text
            If Right(sSearch, 1) = "." Then sSearch = Left(sSearch, Len(sSearch) - 1)
        
            vTemp = Split(sSearch)
            
            For i = LBound(vTemp) To UBound(vTemp)
                If InStr(sFind, UCase(vTemp(i)) & "#") > 0 Then
                    rCell.Offset(0, 1).Value = rCell.Offset(0, 1).Value & vTemp(i) & ";"
                End If
            Next i
        
            'remove last added ";"
            If Len(rCell.Offset(0, 1)) > 0 Then rCell.Offset(0, 1).Value = Left(rCell.Offset(0, 1), Len(rCell.Offset(0, 1)) - 1)
        Next
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Feb 2012
    Posts
    46
    Location
    HI Paul_Hossler ,

    The requirement is to find out even if the find word is of two words.

    The Cat
    The car
    Jacob
    in Circles
    so sorry
    the Door
    a Picture
    Stretched
    Any thing

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I misunderstood

    This should be better

    Option Explicit
    
    
    Sub SearchWords()
        Dim rCell As Range, rFind As Range, rSearch As Range
        Dim vFind As Variant, vTemp As Variant
        Dim iFind As Long
        Dim sFind As String, sSearch As String
        
        'if range not selected then get out
        If Not TypeOf Selection Is Range Then Exit Sub
    
    
        Set rFind = Worksheets("Words to find").Cells(1, 1).CurrentRegion
        Set rFind = rFind(2, 1).Resize(rFind.Rows.Count - 1, 1)
        sFind = Join(Application.WorksheetFunction.Transpose(rFind), "#")
        vFind = Split(sFind, "#")
        
        
        For Each rCell In Selection.Columns(1).Cells
                
            sSearch = UCase(rCell.Text)
            If Right(sSearch, 1) = "." Then sSearch = Left(sSearch, Len(sSearch) - 1)
            
            For iFind = LBound(vFind) To UBound(vFind)
                If InStr(sSearch, UCase(vFind(iFind))) > 0 Then
                    rCell.Offset(0, 1).Value = rCell.Offset(0, 1).Value & vFind(iFind) & ";"
                End If
            Next iFind
        
            'remove last added ";"
            If Len(rCell.Offset(0, 1)) > 0 Then rCell.Offset(0, 1).Value = Left(rCell.Offset(0, 1), Len(rCell.Offset(0, 1)) - 1)
        Next
    End Sub
    Attached Files Attached Files
    Last edited by Paul_Hossler; 04-17-2021 at 07:34 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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