+ Reply to Thread
Results 1 to 13 of 13

Find and highlight cells in workbook that is NOT an exact match to the cell content

Hybrid View

  1. #1
    Registered User
    Join Date
    08-09-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Find and highlight cells in workbook that is NOT an exact match to the cell content

    Hi,

    I am trying to run a macro that will search for a word/words that are not an exact match to the target cell and then highlight the cell. I have a demo attached that runs a macro with an input box that works well but only with an exact match.

    For example: I would like to search for "list" but any cell containing "list" will be highlighted e.g. "Check list" or "Quality List" or even "holistic" etc.

    Would anyone be able to help?

    Thanks

    Andy
    Attached Files Attached Files

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Find and highlight cells in workbook that is NOT an exact match to the cell content

    in your code, just change
    LookAt:=xlWhole
    to
    LookAt:=xlPart

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Find and highlight cells in workbook that is NOT an exact match to the cell content

    If your code works for the exact match, to do the same for partial match,
    Change
    xlWhole
    INTO
    xlPart

  4. #4
    Registered User
    Join Date
    08-09-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Find and highlight cells in workbook that is NOT an exact match to the cell content

    Thanks heaps for that millz & AB33, that works a treat!

    The only problem I have now is that there is no option to find the next cell containing the search criteria (similar to normal "Find Next" function). Is there anyway to add this in?

    Thanks

  5. #5
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Find and highlight cells in workbook that is NOT an exact match to the cell content

    This is what was recorded by Excel when I made it find for "1" and clicking Find Next 4 times.
        Cells.Find(What:="1", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False).Activate
        Cells.FindNext(After:=ActiveCell).Activate
        Cells.FindNext(After:=ActiveCell).Activate
        Cells.FindNext(After:=ActiveCell).Activate

  6. #6
    Registered User
    Join Date
    08-09-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Find and highlight cells in workbook that is NOT an exact match to the cell content

    Thanks for that millz; this only looks for "1" though. Are you able to show how to incorporate the code to "Find" and "Find Next" for the search value?

  7. #7
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Find and highlight cells in workbook that is NOT an exact match to the cell content

    I don't know where do you need to add that in

  8. #8
    Forum Contributor
    Join Date
    08-14-2012
    Location
    USA
    MS-Off Ver
    Excel 2007, MS 365 (Windows 10 Pro 64-bit)
    Posts
    818

    Re: Find and highlight cells in workbook that is NOT an exact match to the cell content

    Hi Andy,
    This is not exactly you want but hope you like it.

    Regards,
    tt3
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Find and highlight cells in workbook that is NOT an exact match to the cell content

    Hi Andy,

    Try this:

    Option Explicit
    
    'Written by Trebor76
    'Visit my website www.excelguru.net.au
    
    'Colour all cells that contain the data (in whole or part) from the 'txtValue' text box
    
    'http://www.excelforum.com/excel-programming-vba-macros/949386-find-and-highlight-cells-in-workbook-that-is-not-an-exact-match-to-the-cell-content.html
    
    Private Sub UserForm_Initialize()
    
        Me.txtValue.SetFocus
    
    End Sub
    Sub cmdSearch_Click()
    
        Dim rngFoundRange As Range, _
            rngFoundCell As Range, _
            rngInterSectRange As Range
        Dim strFirstAddress As String
        Dim varSearchFor As Variant
        Dim wstMySheet As Worksheet
        
        varSearchFor = Me.txtValue
        
        If varSearchFor = False Then
            Exit Sub
        End If
        
        Application.ScreenUpdating = False
        
        For Each wstMySheet In ThisWorkbook.Sheets
            With wstMySheet.Cells
                Set rngFoundCell = .Find(varSearchFor, LookIn:=xlValues)
                If Not rngFoundCell Is Nothing Then
                    strFirstAddress = rngFoundCell.Address
                    Set rngFoundRange = rngFoundCell
                    Do
                        Set rngFoundCell = .FindNext(rngFoundCell)
                        Set rngInterSectRange = Application.Intersect(rngFoundCell, rngFoundRange) 'This is based on this thread: http://www.exceltip.com/st/Determine_if_a_cell_is_within_a_range_using_VBA_in_Microsoft_Excel/484.html
                        If rngInterSectRange Is Nothing Then 'Ensure duplicated cell addresses are not appended to the 'rngFoundRange' range
                            Set rngFoundRange = Union(rngFoundRange, rngFoundCell)
                        End If
                    Loop While Not rngFoundCell Is Nothing And rngFoundCell.Address <> strFirstAddress
                    rngFoundRange.Interior.Color = vbYellow
                Else 'Use this if there's no matches
                End If
            End With
            Set rngFoundRange = Nothing 'Clear the range for the next tab.
        Next wstMySheet
        
        Application.ScreenUpdating = True
        
        MsgBox "All applicable matches have now been highlighted.", vbInformation
    
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  10. #10
    Registered User
    Join Date
    08-09-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Find and highlight cells in workbook that is NOT an exact match to the cell content

    Thanks for all your input everyone, it's much appreciated.

    Robert - Thanks for the code but I get an error message at the "varSearchFor = Me.txtValue" point it seems. Apologies but I am extremely new to VBA and so am unable to read the code - I may be missing the obvious. Any help would be great.

    Thanks

  11. #11
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Find and highlight cells in workbook that is NOT an exact match to the cell content

    Hi Andy,

    Not sure what is the issue is as it works fine for me

    Have a look at the attached where I've incorporated my code to your form.

    Regards,

    Robert
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    08-14-2012
    Location
    USA
    MS-Off Ver
    Excel 2007, MS 365 (Windows 10 Pro 64-bit)
    Posts
    818

    Re: Find and highlight cells in workbook that is NOT an exact match to the cell content

    Hi Andy,
    Try this one.

    Regards,
    tt3
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    08-09-2013
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Find and highlight cells in workbook that is NOT an exact match to the cell content

    Thanks for the input again but I think I may not have explained exactly what I am after. Below is a breakdown of what I would ideally like:

    1) Search for a partial match via an input box
    2) For the search to find the first occurrence that includes the search text, highlight the cell and move to the relevant worksheet (there are multiple worksheets that need to be searched)
    3) If the first match is not the correct one I would like a method that will jump to the next cell in the workbook that contains the search text (similar to the "Find Next" button through the Find & Replace tool already incorporated in Excel)

    Although the previous macros highlight all the cells they do not take the user to the correct sheet and so the user would have to search through every tab and scroll down each page to find the highlighted cells. Also, all occurrences are highlighted simultaneously instead of one at a time.

    This may be a little complex and I am thinking that maybe I will just have to use the Find & Replace and live with the cells not being highlighted. However, if anyone thinks there is a way to solve the issue any further feedback would be greatly appreciated.

    Thanks

+ 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] How to find the exact cell content in a column
    By yeshwant_sur in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-11-2012, 02:13 AM
  2. [SOLVED] Highlight a cell if its content doesn't match another cells content
    By noxygen in forum Excel General
    Replies: 4
    Last Post: 04-12-2012, 04:36 AM
  3. Find an exact duplicate match on concatenated cells
    By madball87 in forum Excel General
    Replies: 2
    Last Post: 10-04-2011, 06:07 AM
  4. Highlight entire row instead of cell when find a match?
    By fcb1900 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-02-2010, 07:03 AM
  5. Find exact match in cell with multiple strings
    By Garage23 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-16-2010, 07:23 AM

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