appreciate you reading this post. I’m sure this is a simple one, but I’m new to VBA coding.

I’ve got a list of words between cell D23:D52 in a spreadsheet that are all separated by a hyphen. IE: “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 of the hyphen as ITALICS like this: “word1 word2 – word3 word4

I can only figure out how to either italicize every word or make every word bold. I figure there has to be a way to split the words into italics and bold using the hyphen as a delimiter.

Is there a code that would help me achieve this goal?


New contributor

leatherface316 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


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
End Sub

Microsoft documentation:

Range.Characters property (Excel)

Split function