1
votes

I am trying to underline, bold and italicized certain portions of text within a cell. My first line of text starts at line 2, where I am trying to bold the second and third line of this cell. The last part of line 2, I am trying to italicized this. Line 5 needs to be underlined. I have a list of these that I am trying to go through, so I started with a 'for loop'. I can get the first two lines to be bold, but I have trouble getting the italic and underline portion right.

Here's what I'm starting with:

enter image description here

Here's what I'm going for:

enter image description here

Public Sub HUBSpecificStyle()

Dim LongRow     As Long

Dim cel As Range
Dim rng As Range
Dim varContent, varFirstRow
Dim FoodRange As Range


'Looping through all cells in selection
For Each cel In Range("A2:A")
    varContent = Split(cel.Value, Chr(10)) ' Spliting cell contents by newline character
    
    
    With cel
    
        varContent = Split(cel.Value, Chr(10)) ' Spliting cell contents by newline character
        .Characters(1, (Len(varContent(0) & varContent(1)) + 1)).Font.Bold = True
        .Characters(Len(varContent(1)) + 1, Len(varContent(2)) + 2).Font.Bold = True
    Debug.Print cel
    
    End With

Next cel

End Sub

1

1 Answers

1
votes

Untested but here's one potential approach

Dim arr, i as long, pos as long, txt, length, dashPos

arr = Split(cel.Value, Chr(10)) ' Spliting cell contents by newline character

pos = 1    
For i = 1 to UBound(arr)+1
    
    txt = arr(i-1)
    length = Len(txt)

    'check which line we're on...
    Select Case i
        Case 2
            cel.Characters(pos, length).Font.Bold = True
            dashPos = instr(txt, "-")
            cel.Characters(pos+dashPos, length-dashPos).Font.Italic = True 'check my math!
        Case 3: cel.Characters(pos, length).Font.Bold = True 
        Case 5: cel.Characters(pos, length).Font.UnderLine = True
    End Select

    pos = pos + len(txt) + 1 'start position for next line
Next i
```lang-vba