Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

excel - Is there a way to use underline, bold and Italic in a single cell?

I am fairly new to the VBA world. I was wondering if there is any way that you could use underline, bold and Italic in a single cell. In this example, I would like just the first and second line to be bold, with the name coming 2 hyphens after the first word, and the third line underline. Not entirely sure if this is possible. The first cell shows what I have. The second cell shows what I'm looking for.

enter image description here

question from:https://stackoverflow.com/questions/65879675/is-there-a-way-to-use-underline-bold-and-italic-in-a-single-cell

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to perform this on several cells then you can use below code as a starting point. I have provided comments in the code which should assist you to understand.

Public Sub HUBSpecificStyle()
    Dim rng As Range
    Dim varContent, varFirstRow
    
    '\Loop through all cells in selection
    For Each rng In Range("A2:B2") '\ Set your range reference here
        varContent = Split(rng.Value, Chr(10)) '\ Split cell contents by newline character
        With rng
            '\ First two lines of row bold
            .Characters(1, Len(varContent(0) & Chr(10) & varContent(1))).Font.Bold = True
            '\Third line underline
            .Characters(Len(varContent(0) & Chr(10) & varContent(1) & Chr(10)) + 1, Len(varContent(2))).Font.Underline = True
            '\ Split first line with hyphens
            varFirstRow = Split(varContent(0), "-")
            '\Third part italic
            .Characters(Len(varFirstRow(0) & "-" & varFirstRow(1) & "-") + 1, Len(varFirstRow(2))).Font.Italic = True
        End With
    Next rng

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...