Excel VBA 宏用单元格中的粗体文本替换 Html 粗体标签
Excel VBA Macro Replace Html Bold Tag With Bolded Text In Cell
我有以下内容:
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
这可以循环单元格并使 标签之间的所有文本加粗。
但是,这仍然会在单元格中留下标签。
我需要一种在特定单元格中加粗和删除标签的方法。
我尝试添加:
Range("B17").Value = Replace(Range("B17").Value, "<b>", "")
Range("B17").Value = Replace(Range("B17").Value, "</b>", "")
但是,这不仅删除了标签,还删除了粗体。
可以这样做吗?
此代码在删除标签之前首先记录标签的位置。然后,在一个单独的循环中,它将粗体应用于注释的文本位置。
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)
并决定将其保留在那里。我不希望代码 over-write 原始文本。或许你也有类似的需求。请调整该行以适应。
我有以下内容:
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
这可以循环单元格并使 标签之间的所有文本加粗。 但是,这仍然会在单元格中留下标签。
我需要一种在特定单元格中加粗和删除标签的方法。 我尝试添加:
Range("B17").Value = Replace(Range("B17").Value, "<b>", "")
Range("B17").Value = Replace(Range("B17").Value, "</b>", "")
但是,这不仅删除了标签,还删除了粗体。
可以这样做吗?
此代码在删除标签之前首先记录标签的位置。然后,在一个单独的循环中,它将粗体应用于注释的文本位置。
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)
并决定将其保留在那里。我不希望代码 over-write 原始文本。或许你也有类似的需求。请调整该行以适应。