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
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
try any of the following function in vba
instr
worksheetfucntion.search
see vba help
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks