leatherface316
leatherface316

Reputation: 1

Format words to the left of a hyphen as bold and words to the right as italics

I've a list of words between cell D23:D52 that are separated by a hyphen.

Example:

word1 word2 - word3 word4

I'd like to format the words to the left of the hyphen as BOLD and the words to the right as ITALICS:

word1 word2 - word3 word4

I can only figure out how to either italicize every word or make every word bold.

Is there a way to split the words into italics and bold using the hyphen as a delimiter?

Upvotes: 0

Views: 83

Answers (1)

taller
taller

Reputation: 18778

Please try.

Option Explicit

Sub demo()
     Dim c As Range, aTxt
     For Each c In Range("D23:D52")
        If Len(c.Value) > 0 Then
            If InStr(c.Value, "-") > 0 Then
                aTxt = Split(c.Value, "-")
                c.Characters(1, Len(aTxt(0))).Font.Bold = True
                c.Characters(Len(aTxt(0)) + 2, Len(aTxt(1))).Font.Italic = True
            End If
        End If
     Next
End Sub

Microsoft documentation:

Range.Characters property (Excel)

Split function

Upvotes: 1

Related Questions