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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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
 
Upvote 0
@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!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,195
Members
449,072
Latest member
DW Draft

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