+ Reply to Thread
Results 1 to 3 of 3

VBA to find and select text within text and highlight

Hybrid View

  1. #1
    Registered User
    Join Date
    05-09-2011
    Location
    Bridgeport, Connecticut
    MS-Off Ver
    Excel 2010
    Posts
    4

    VBA to find and select text within text and highlight

    I have adopted code from this forum to find all cells with text that matches user-entered text and which opens the color palette so users can choose the color to highlight that text. However, while the code does all this very well, it does not find text within text. The built-in Excel search function does find text within text, and that it what I am trying to get the code to do, with the additional benefit of highlighting that text (or at least the cell in which it is contained). I cannot figure out how to get the VBA search to work when the search text has at least one other character or number in the cell.

    This is the code I have:
    Sub HighlightCellsChooseColor()
    
    'This will search the entire workbook for all instances of the search text.
    'It will highlight each instance in your choice of color. This search is
    'not case-sensitive, but it will not locate search text if its cell
    'contains other text,including a formula."
    
    Dim i As Long
    Dim Fnd As String
    Dim fCell As Range
    Dim ws As Worksheet
    Dim Color As Integer
    Dim rngCurr As Range
    
    Fnd = InputBox("Enter text to search" & vbCr & vbCr _
           & "Click OK to search the entire workbook for all instances of the search text.  Each instance will be highlighted in yellow. This search is not case-sensitive, but it will not locate search text if its cell contains other text, including a formula.")
    
    If Fnd = vbNullString Then
               Exit Sub
    End If
    
    Application.ScreenUpdating = False
    Application.Dialogs(xlDialogPatterns).Show 'shows color palette
    Color = ActiveCell.Interior.ColorIndex 'stores index number of selected color
    ActiveCell.Interior.ColorIndex = xlNone 'since previous line colors cell where cursor pointer is, this removes the shading from that cell
    
    Application.ScreenUpdating = True
    
    For Each ws In Worksheets
        With ws
            Set fCell = .Range("A1")
            For i = 1 To WorksheetFunction.CountIf(.Cells, Fnd)
                Set fCell = .Cells.Find(What:=Fnd, After:=fCell, LookIn:=xlValues, _
                                        LookAt:=xlPart, SearchOrder:=xlByRows, _
                                        SearchDirection:=xlNext, MatchCase:=False)
                If fCell Is Nothing Then
                    MsgBox Fnd & " not on sheet !!"
                    Exit For
                Else
       
                   With fCell
                        .Interior.ColorIndex = Color 'applies color user selected from palette
                   End With
                End If
            Next i
        End With
    Next ws
    End Sub
    Last edited by Leith Ross; 05-10-2011 at 06:49 PM. Reason: Added Code Tags

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: VBA to find and select text within text and highlight

    look at this code and try it. Does it works like you want it to? I'm not sure.

    Sub HighlightCellsChooseColor()
    
    'This will search the entire workbook for all instances of the search text.
    'It will highlight each instance in your choice of color. This search is
    'not case-sensitive, but it will not locate search text if its cell
    'contains other text,including a formula."
    
    Dim i As Long
    Dim Fnd As String
    Dim fCell As Range
    Dim ws As Worksheet
    Dim Color As Long
    Dim rngCurr As Range
    
    Fnd = InputBox("Enter text to search" & vbCr & vbCr _
           & "Click OK to search the entire workbook for all instances of the search text.  Each instance will be highlighted in yellow. This search is not case-sensitive, but it will not locate search text if its cell contains other text, including a formula.")
    
    If Fnd = vbNullString Then
               Exit Sub
    End If
    
    Application.ScreenUpdating = False
    Application.Dialogs(xlDialogPatterns).Show 'shows color palette
    Color = ActiveCell.Interior.Color 'stores index number of selected color
    ActiveCell.Interior.Color = xlNone 'since previous line colors cell where cursor pointer is, this removes the shading from that cell
    
    Application.ScreenUpdating = True
    
    
        Cells.Find(What:=Fnd, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = Color
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
            
    first_pos = ActiveCell.Address 'first cell found
    
    Cells.FindNext(After:=ActiveCell).Activate 'find the next cell if any
    
    While ActiveCell.Address <> first_pos 'while nect cell is not the first one
    
    
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = Color
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        Cells.FindNext(After:=ActiveCell).Activate
        
    Wend
    
    End Sub

  3. #3
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    678

    Re: VBA to find and select text within text and highlight

    try any of the following function in vba

    instr
    worksheetfucntion.search

    see vba help

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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