此代码在删除它们之前首先记录标签的位置。然后,在一个单独的循环中,它会将粗体字体应用于注明的文本位置。
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
我觉得有点慢。因此,我想Application.ScreenUpdating = False
在它运行时暂停屏幕更新(),但没有。原因是该过程仅格式化单个单元格。您可能会从另一个过程中调用它,该过程循环遍历列中的所有单元格,依次将每个单元格输入到上述过程中。使用类似的代码SetCharsBold Range("F1")
。屏幕控制应该在那个过程中完成,延迟更新直到它的循环运行。
我忘了Cell.Offset(18)
从代码中删除,并决定将其留在那里。我不希望代码覆盖原始文本。也许你有类似的需求。请调整那条线以适应。