简体   繁体   English

Excel VBA 宏在单元格中用粗体文本替换 Html 粗体标记

[英]Excel VBA Macro Replace Html Bold Tag With Bolded Text In Cell

I have the following:我有以下几点:

s = 1
f = 1
For i = 1 To UBound(Split(Range("B17").Value, "<b>"))
    s = InStr(f, Range("B17").Value, ("<b>"))
    f = InStr(s, Range("B17").Value, ("</b>"))
    Range("B17").Characters(s, f - s + 1).Font.FontStyle = "Bold"
Next i

This works to loop a cell and make all text between这适用于循环单元格并使所有文本之间 tags bolded.标签加粗。 However, this also still leaves behind the tags in the cell.但是,这也仍然会在单元格中留下标签。

I need a way to bold between AND remove the tags from a specific cell.我需要一种在特定单元格中加粗并删除标签的方法。 I tried to add:我尝试添加:

Range("B17").Value = Replace(Range("B17").Value, "<b>", "")
Range("B17").Value = Replace(Range("B17").Value, "</b>", "")

BUT, this not only removed the tags, it also removed the bold font.但是,这不仅删除了标签,还删除了粗体。

Is it possible to do this?是否有可能做到这一点?

This code first notes the position of the tags before removing them.这段代码在移除标签之前首先记录标签的位置。 Then, in a separate loop, it applies bold font to the noted text positions.然后,在一个单独的循环中,它将粗体字体应用于标注的文本位置。

Private Sub SetCharsBold(Cell As Range)
    ' 086

    Const Tag       As String = "<b>"       ' tag string: start
    Const Tend      As String = "</b>"      ' tag string: end
    Const Pstart    As Integer = 0          ' vector index of Pos()
    Const Pend      As Integer = 1          ' vector index of Pos()
    
    Dim Cv          As String               ' Cell value
    Dim Cnt         As Integer              ' instances of bold expressions
    Dim Pos()       As Variant              ' string positions: 0 = start, 1 = End
    Dim f           As Integer              ' loop counter: Cnt
    
    Cv = Cell.Value
    Cnt = (Len(Cv) - Len(Replace(Cv, Tag, ""))) / 3
    ReDim Pos(Cnt, Pend)
    For f = 1 To Cnt
        Pos(f, Pstart) = InStr(Cv, Tag)
        Cv = Left(Cv, Pos(f, Pstart) - 1) & Mid(Cv, Pos(f, Pstart) + Len(Tag), Len(Cv))
        Pos(f, Pend) = InStr(Cv, Tend) - 1
        Cv = Left(Cv, Pos(f, Pend)) & Mid(Cv, Pos(f, Pend) + Len(Tend) + 1, Len(Cv))
    Next f
    
    With Cell.Offset(18)
        .Font.Bold = False
        .Value = Cv
        For f = 1 To Cnt
            .Characters(Pos(f, Pstart), Pos(f, Pend) - Pos(f, Pstart) + 1).Font.Bold = True
        Next f
    End With
End Sub 

I thought it's a bit slow.我以为它有点慢。 Therefore I wanted to pause screen updating ( Application.ScreenUpdating = False ) while it runs but refrained.因此,我想在它运行时暂停屏幕更新( Application.ScreenUpdating = False ),但又克制住了。 The reason is that the procedure just formats a single cell.原因是该过程只是格式化单个单元格。 You would probably call it from another procedure that loops through all your cells in a column, feeding each one to the above proc in turn.您可能会从另一个过程调用它,该过程循环遍历列中的所有单元格,依次将每个单元格提供给上述过程。 Use code like SetCharsBold Range("F1") .使用像SetCharsBold Range("F1")这样的代码。 The screen control should be done in that procedure, delaying the update until its loop has run.屏幕控制应该在那个过程中完成,延迟更新直到它的循环运行。

I forgot to remove Cell.Offset(18) from the code and decided to leave it there on second thought.我忘记从代码中删除Cell.Offset(18)并决定将其留在那里再考虑一下。 I didn't want the code to over-write the original texts.我不希望代码覆盖原始文本。 Perhaps you have a similar need.或许你也有类似的需求。 Please adjust that line to suit.请调整那条线以适应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM