colored words after search by inputbox

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi
i try color this word "in " into any cell by blue and bold after search inputbox
this is my code but not works well
output
1.JPG





Code:
Sub highlighttext()
    Dim cell As Range
    Dim cellRange As Range
    Dim textStart As Integer
    Dim wordToFind As String
    Set cellRange = Selection
   
    Application.ScreenUpdating = False
   
    ' Ask the user what word they want to highlight
    wordToFind = InputBox(Prompt:="What word would you like to highlight?")
   
    For Each cell In cellRange
        textStart = InStr(cell.Value, wordToFind)
       
        cell.Characters(textStart, Len(wordToFind)).Font.Color = RGB(250, 0, 0)
    Next cell
   
    Application.ScreenUpdating = True

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You say that it doesn't work well, but don't say what it does that you don't like.
The only thing that I notice is that it does not turn all the characters black before coloring the new string. If that is what you want (multiple colored strings in the same cell) great, otherwise, the code should change the color to black before starting the loop.
 
Upvote 0
i got this code but i would adjusting to become any word i search colored and bold by blue in any cell
 
Upvote 0
i got this code but i would adjusting to become any word i search colored and bold by blue in any cell
Is this what you mean ?

VBA Code:
    For Each cell In cellRange
      textStart = InStr(cell.Value, wordToFind)
       With cell.Characters(textStart, Len(wordToFind)).font
            .Color = RGB(0, 0, 255)
            .Bold = True
        End With
    Next cell
 
Last edited:
Upvote 0
Ignore the previous code and use this one whch takes care of the situation when the user enters a space in the inputbox

VBA Code:
Sub highlighttext()

    Dim cell As Range
    Dim cellRange As Range
    Dim textStart As Integer
    Dim wordToFind As String
    Set cellRange = Selection
  
    Application.ScreenUpdating = False
  
    ' Ask the user what word they want to highlight
    wordToFind = InputBox("What word would you like to highlight?", , Selection.Value)
    
    For Each cell In cellRange
        If Len(Replace(wordToFind, " ", "")) Then
            textStart = InStr(1, cell.text, wordToFind)
            With cell.Characters(textStart, Len(wordToFind)).font
                .Color = RGB(0, 0, 255)
                .Bold = True
            End With
        End If
    Next cell
  
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
not exactly jaafar i would color every cell contains my word chose not only in active cell with consider if the word locate in between two words it should the color chose word not all words
 
Upvote 0
not exactly jaafar i would color every cell contains my word chose not only in active cell with consider if the word locate in between two words it should the color chose word not all words

I this this is what you mean :
VBA Code:
Sub highlighttext()

    Dim cell As Range
    Dim cellRange As Range
    Dim textStart As Integer
    Dim wordToFind As String
    Set cellRange = Selection
  
    Application.ScreenUpdating = False
  
    ' Ask the user what word they want to highlight
    wordToFind = InputBox("What word would you like to highlight?")
    
    For Each cell In cellRange
        If Len(Replace(wordToFind, " ", "")) Then
            textStart = InStr(1, cell.text, wordToFind)
            With cell.Characters(textStart, Len(wordToFind)).font
                .Color = RGB(0, 0, 255)
                .Bold = True
            End With
        End If
    Next cell
  
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
hi, jaffar your code is something wrong you can see my images
1.JPG

2.JPG


and i wouldn't specify cells , i would color without specify from me
 
Upvote 0
I see:

Try this :
VBA Code:
Sub highlighttext()

    Dim cell As Range
    Dim cellRange As Range
    Dim textStart As Integer
    Dim wordToFind As String
    Set cellRange = Selection
  
    Application.ScreenUpdating = False
  
    ' Ask the user what word they want to highlight
    wordToFind = InputBox("What word would you like to highlight?")
    
    For Each cell In cellRange
        If Len(Replace(wordToFind, " ", "")) Then
            textStart = InStr(1, cell.text, wordToFind)
            If textStart Then
                With cell.Characters(textStart, Len(wordToFind)).font
                    .Color = RGB(0, 0, 255)
                    .Bold = True
                End With
            End If
        End If
    Next cell
  
    Application.ScreenUpdating = True

End Sub

Note that if the cell contains more than one instance of the search word it will only highlight the first one .
 
Upvote 0
great work jaafar now it work but i have ever said repeatedly about specify cell if is possible i don't interfere from me i'm talking about this
VBA Code:
    Set cellRange = Selection

i would color directly without select cell range
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,483
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top