Italicize part of text string (Excel -VBA Macro)

kdsan

New Member
Joined
Apr 17, 2020
Messages
3
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hello - I am not fluent in vba macros....
I have a spreadsheet that contains several cells that contain text that contain the phrase "Select". For example:

"This is part of the string. (Select only its part to italicize)".

Here's the catch. The portion of the string that starts with "(Select" may vary from line to line.
(Select one)
(Select up to 5)
etc...

I tried using find and replace with wild cards but it applies the italics to the entire string.

I've looked up some VBA code but haven't gotten it to work.

Any suggestions are welcome. Thanks!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,622
Office Version
  1. 2007
Platform
  1. Windows
Assuming your data is in cell A1 down, try this:

VBA Code:
Sub Italicize_part_text()
  Dim c As Range, n As Long, m As Long
  For Each c In Range("A1", Range("A" & Rows.Count).End(3))
    n = InStr(1, c.Value, "Select", vbTextCompare)
    If n > 0 Then
      m = InStr(1, c.Value, ")", vbTextCompare)
      If m = 0 Then m = Len(c.Value) Else m = m - n
      c.Characters(Start:=n, Length:=m).Font.FontStyle = "Italic"
    End If
  Next c
End Sub
 

kdsan

New Member
Joined
Apr 17, 2020
Messages
3
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
@DanteAmor thank you! Worked like a chart. Wish I had just come here instead of spending 2+ hours trying to figure it out on my own!
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,622
Office Version
  1. 2007
Platform
  1. Windows
I'm glad to help you. Thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,943
Messages
5,639,116
Members
417,073
Latest member
CBG_Christian

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
Top