Color and underline a single word within a cell

giwdal

New Member
Joined
Jun 3, 2015
Messages
2
What macro can I use to both make a specific word within a cell both red and underlined?

For example, I would like to make the word "must" wherever it is in any cell both red and underlined.

Thank you!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Maybe this will give you a head start...
the following code underlines and makes the font red for the word "must" on the selected cell. So you might wanna loop or modify based on your needs.

Code:
Sub test()
    With ActiveCell.Characters(Start:=WorksheetFunction.Find("must", ActiveCell), Length:=4).Font
        .Underline = xlUnderlineStyleSingle
        .Color = -16776961
    End With
End Sub
 
Upvote 0
giwdal,

Welcome to the MrExcel forum.

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?


Here is a beginning macro solution, based on iggydarsa's code, for you to consider.


The after the macro screenshot is not showing the word must correctly - please try the macro on similar data in column A, for 5 rows, A1:A5.


Sample raw data:


Excel 2007
A
1I would like to make the word "must" wherever it is in any cell both red and underlined.
2make the word mustard wherever
3make the word is not here
4make the word "must" wherever
5we must stop here
6
Sheet1


After the macro:


Excel 2007
A
1I would like to make the word "must" wherever it is in any cell both red and underlined.
2make the word mustard wherever
3make the word is not here
4make the word "must" wherever
5we must stop here
6
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ColorUnderlineWord()
' hiker95, 06/03/2015, ME859072
Dim c As Range
For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
  If InStr(c, "must") Then
    With c.Characters(Start:=WorksheetFunction.Find("must", c), Length:=4).Font
      .Underline = xlUnderlineStyleSingle
      .Color = vbRed
    End With
  End If
Next c
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ColorUnderlineWord macro.
 
Upvote 0

Forum statistics

Threads
1,203,553
Messages
6,056,063
Members
444,841
Latest member
SF_Marnie

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