How do you underline, bold and italicized certain portions of text within a range for each cell?

泪湿孤枕 提交于 2021-02-10 14:33:27

问题


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:

Here's what I'm going for:

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:


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    


来源:https://stackoverflow.com/questions/65896436/how-do-you-underline-bold-and-italicized-certain-portions-of-text-within-a-rang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!