我正在从工作表中的文本列表生成 XML,但我不知道如何检查当前单元格中是否包含粗体字。我需要做的是检查 A 列中的每个单元格,将文本读入字符串,如果我点击任何粗体字,请 在其周围添加标签。
我知道您可以逐个字符地读取单元格内容,但不能读取其格式。
任何帮助将不胜感激!
我正在从工作表中的文本列表生成 XML,但我不知道如何检查当前单元格中是否包含粗体字。我需要做的是检查 A 列中的每个单元格,将文本读入字符串,如果我点击任何粗体字,请 在其周围添加标签。
我知道您可以逐个字符地读取单元格内容,但不能读取其格式。
任何帮助将不胜感激!
这是一种可用于检查单元格是否具有
NULL
TRUE
FALSE
例子
Sub Sample()
Debug.Print Range("A1").Font.Bold
Debug.Print Range("A2").Font.Bold
Debug.Print Range("A3").Font.Bold
End Sub
要检查单元格是否有任何粗体字符,您也可以使用此函数(来自 VBA 或 Worksheet)
'~~> This is an additional function which will return...
'~~> TRUE if Cell has mixed/all chars as bold
'~~> FALSE if cell doesn't have any character in bold.
'~~> This can also be used as a worksheet function.
Function FindBoldCharacters(ByVal aCell As Range) As Boolean
FindBoldCharacters = IsNull(aCell.Font.Bold)
If Not FindBoldCharacters Then FindBoldCharacters = aCell.Font.Bold
End Function
截屏
您可以使用它.Characters().Font.FontStyle
来检查每个字符是否为粗体。使用上面的 RangeA1
示例。
Sub Sample()
For i = 1 To Len(Range("A1").Value)
Debug.Print Range("A1").Characters(i, 1).Font.FontStyle
Next i
End Sub
截图
修改后的代码
Sub Sample()
For i = 1 To Len(Range("A1").Value)
If Range("A1").Characters(i, 1).Font.FontStyle = "Bold" Then
Debug.Print "The " & i & " character is in bold."
End If
Next i
End Sub
截屏