colored words after search by inputbox

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,433
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
 
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
What do you mean ? the code doesn' select any cells.
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
i mean the code not work if i don't interfere from me by specify the filling cells what i want color them so, is there any way do that without specify the filling cells
 
Upvote 0
i mean the code not work if i don't interfere from me by specify the filling cells what i want color them so, is there any way do that without specify the filling cells

I am sorry but I don't quite understand what you mean. Do you want to apply the formatting to a predifined range ?
 
Upvote 0
i have succeeded my adjusting and works perfectly i adjust this

VBA Code:
Set cellRange = Selection

to this
Code:
Set cellRange = ActiveSheet.UsedRange

i have last thing if you don't mind how can i return black color for old data colored by blue when i color a new data
 
Upvote 0
If your initial data has black font throughout the entire used range then you could try this
VBA Code:
Sub highlighttext()

    Dim cell As Range
    Dim cellRange As Range
    Dim textStart As Integer
    Dim wordToFind As String
     
    Application.ScreenUpdating = False
   
    Set cellRange = ActiveSheet.UsedRange
   
    cellRange.font.Color = 0
    cellRange.font.Bold = 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
 
Upvote 0
well done jaffar thanks for every thing , now your code is complete
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,393
Members
449,446
Latest member
CodeCybear

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