excel - Vba检查单元格中是否部分粗体

我正在从工作表中的文本列表生成 XML,但我不知道如何检查当前单元格中是否包含粗体字。我需要做的是检查 A 列中的每个单元格,将文本读入字符串,如果我点击任何粗体字,请在其周围添加 标签。

我知道您可以逐个字符地读取单元格内容,但不能读取其格式。

任何帮助将不胜感激!

最佳答案

这是一种检查单元格是否有的方法

  1. 加粗的混合字符。在这种情况下,它将返回 NULL
  2. 所有字符都是粗体。在这种情况下,它将返回 TRUE
  3. 没有一个字符是粗体的。在这种情况下,它将返回 FALSE

示例

Sub Sample()
    Debug.Print Range("A1").Font.Bold
    Debug.Print Range("A2").Font.Bold
    Debug.Print Range("A3").Font.Bold
End Sub

要检查单元格是否有任何粗体字符,您也可以使用此函数(来自 VBA 或工作表)

'~~> 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 来检查每个字符是否为粗体。使用上面的 Range A1 示例。

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

截图

https://stackoverflow.com/questions/16209572/

相关文章:

c# - 使用 string.format 和 List.Count() 进行动态字符串格式化

android - 对话框中忽略的 Html 格式标记

html - 如何让 IntelliJ IDEA 保持 HTML 嵌套选项卡?

excel - 如何通过VBA代码获取Excel 2012条件格式的色标制作的颜色

android - Eclipse AndroidManifest.xml 格式

c# - 如果语句突然被 Visual Studio 2010 在下一行格式化

javascript - Kendo UI 货币格式

iphone - NSDictionary `description` 格式化问题 - 将结构视为

java - 从java中的字符串解析带有正/负前缀的数字

java - 将 lenient 设置为 false 时的 SimpleDateFormat 异常