VBA code in excel to made text between tags bold

2020-05-06 14:38发布

I have a csv file which includes the html tags < b > and <\ b > to signify bold text. (I.e several words between these tags, in a longer block of text within the cell, should be bold). Is there a way using vba code in excel to strip the tags, and make the text between the tags bold?

Note - There are sometime multiple sets of tags within a given cell.

1条回答
走好不送
2楼-- · 2020-05-06 15:01

This should do what you want:

Sub BoldTags()
Dim X As Long, BoldOn As Boolean
BoldOn = False 'Default from start of cell is not to bold
For X = 1 To Len(ActiveCell.Text)
    If UCase(Mid(ActiveCell.Text, X, 3)) = "<B>" Then
        BoldOn = True
        ActiveCell.Characters(X, 3).Delete
    End If
    If UCase(Mid(ActiveCell.Text, X, 4)) = "</B>" Then
        BoldOn = False
        ActiveCell.Characters(X, 4).Delete
    End If
    ActiveCell.Characters(X, 1).Font.Bold = BoldOn
Next
End Sub

Currently set to run on the activecell, you can just plop it in a loop to do a whole column. You can easily adapt this code for other HTML tags for Cell formatting (ie italic etc)

This was in the cell I tested on (minus the space after <): Sample < b>Te< /b>st of < B>bolding< /B> end

The result was: Sample Test of bolding end

Hope that helps

查看更多
登录 后发表回答