Formatting a substring within a cell only

adim654

New Member
Joined
Aug 23, 2013
Messages
10
Hi all,
The task I have requires repeatedly formatting specific words within cells, rather than the whole cell's text. I thought i'd try to write a macro to automate this, but can't seem to find anything about how to format only specific substrings in a cell. I was hoping to have the macro work something like this: select the word you want to format, run the code and it would apply the formatting to that word.

Does anyone know what the method would be for achieving this? I would think that there would be something similar to "CurrentSelection", which would be the most straightforward way to go about this.
Thanks in advance,
Alex
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You should use the characters property of the activecell or a specific cell like a small example i wrote here
Code:
Sub FormatOnlyPortion()
    With ActiveCell.Characters(2, 5).Font
        .Bold = True
        .Color = vbBlue
    End With
End Sub
This will change the font color(to blue) and make bold 5 characters starting from character 2 in the active cell. VBA help will give you more info
 
Upvote 0
Hi all,
The task I have requires repeatedly formatting specific words within cells, rather than the whole cell's text. I thought i'd try to write a macro to automate this, but can't seem to find anything about how to format only specific substrings in a cell. I was hoping to have the macro work something like this: select the word you want to format, run the code and it would apply the formatting to that word.

Hi,

I used macro recorder and found same logic what Momentman wrote. I wrote this code which is working in column "A" on sheet "Sheet1" for text only. It colors text to red what you type in inputbox.

Code:
Sub Formatting_substring_within_a_cell_only()


Dim Rng As Range
Dim WordToSearch As String
Dim WordLength As Integer
Dim Cella
Dim StartHere As Integer


Application.ScreenUpdating = False


Set Rng = ThisWorkbook.Sheets("Sheet1").UsedRange
WordToSearch = Application.InputBox("Give word you want to find in cells in column 'A'", , "text", , , , , 2)
WordLength = Len(WordToSearch)


On Error Resume Next
For Each Cella In Rng.Columns(1).Cells
    StartHere = Application.WorksheetFunction.Find(WordToSearch, Sheets("Sheet1").Range(Cella.Address), 1)
    Cella.Characters(Start:=StartHere, Length:=WordLength).Font.Color = vbRed
Next Cella
On Error GoTo 0


Application.ScreenUpdating = True


End Sub
 
Last edited:
Upvote 0
You should use the characters property of the activecell or a specific cell like a small example i wrote here
Code:
Sub FormatOnlyPortion()
    With ActiveCell.Characters(2, 5).Font
        .Bold = True
        .Color = vbBlue
    End With
End Sub
This will change the font color(to blue) and make bold 5 characters starting from character 2 in the active cell. VBA help will give you more info

Thanks for this, this does make sense if I know exactly where the text will be located in each cell, but this is not the case. The words are always in different locations within the cell, hence why I would like something that allows me to run the code on the currently highlighted text. Perhaps this level of control isn't possible?
 
Upvote 0
Thanks for this, this does make sense if I know exactly where the text will be located in each cell, but this is not the case. The words are always in different locations within the cell, hence why I would like something that allows me to run the code on the currently highlighted text. Perhaps this level of control isn't possible?
Did you see KeepTrying's code in Message #3 yet? I did not test it, but it looks like it will work... give it a try and report your results back to us.
 
Upvote 0
why not use a SEARCH/FIND/INSTR function to find the location of the word within the cell and then feed that to the characters property
 
Upvote 0
Did you see KeepTrying's code in Message #3 yet? I did not test it, but it looks like it will work... give it a try and report your results back to us.

I tried it, and it does work well. Does anyone know of a way to apply the formatting to the text that you currently have highlighted though, rather than typing in the string? I appreciate all of the help.
 
Upvote 0
I tried it, and it does work well. Does anyone know of a way to apply the formatting to the text that you currently have highlighted though, rather than typing in the string?
If you mean use the highlighted text to process all the cells in the Column, I do not believe you can do that... when you select text within a cell, Excel is put into Edit Mode... as far as I know, nothing else can be done in Excel or VBA until Edit Mode is exited (which would mean the text would no longer be selected).
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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