Forum Discussion

Nruti Desai's avatar
Nruti Desai
Copper Contributor
May 25, 2018
Solved

Formula does not work for all

I am using =IF(ISNUMBER(SEARCH("RangeName",G2)),RangeName) to find words in a cell for list of words. Once that word is found I want that word to be the result if the words nor found than say FALSE. ...
  • Matt Mickle's avatar
    Matt Mickle
    May 25, 2018

    Try using the below VBA Code to get the result you would like.  I'm attaching a .xlsx workbook as an example since it's not possible to attach macro enabled workbooks.  You will need to insert the code I have provided below into the workbook to see how it works.  Once you grasp the concept you can alter the code to work for your needs.  Hope this helps.

     

    Before:

     

    After:

     

    Code:

    Sub GetWords()
    
        Dim wrdLRow As Integer
        Dim wrdLp As Integer
        Dim CommentLrow As Integer
        Dim CommentLp As Integer
        Dim fndWord As Integer
        Dim Sht As Worksheet
        
        On Error Resume Next 'Suppress Errors... for when we don't find a match
        
        'Define worksheet that has data on it....
        Set Sht = Sheets("Sheet1")
        
        'Get last row for words based on column A
        wrdLRow = Sht.Cells(Rows.Count, "A").End(xlUp).Row
        
        'Get last row for comments based on column C
        CommentLrow = Sht.Cells(Rows.Count, "C").End(xlUp).Row
        
        'Loop through lists and find matches....
        For CommentLp = 2 To CommentLrow
        
            For wrdLp = 2 To wrdLRow
    
                'Look for word...
                fndWord = Application.WorksheetFunction.Search(Sht.Cells(wrdLp, "A"), Sht.Cells(CommentLp, "C"))
                
                'If we found the word....then
                If fndWord > 0 Then
                    Sht.Cells(CommentLp, "D") = Sht.Cells(CommentLp, "D") & "; " & Sht.Cells(wrdLp, "A")
                    fndWord = 0 'Reset Variable for next loop
                End If
                
            Next wrdLp
            
            Sht.Cells(CommentLp, "D") = Mid(Sht.Cells(CommentLp, "D"), 3, Len(Sht.Cells(CommentLp, "D")) - 2)
    
        Next CommentLp
    
    End Sub
    

     

     

Resources