Vba 检查单元格中是否部分加粗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16209572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vba check if partial bolding in cell
提问by user1680260
I'm generating XML from a list of text within a worksheet but I cant figure out how to check if the current cell has a bold word within it. What I need to do is check each cell in column A, read the text into a string, if I hit any bold words add the tags around it.
我正在从工作表中的文本列表生成 XML,但我不知道如何检查当前单元格中是否有粗体字。我需要做的是检查 A 列中的每个单元格,将文本读入一个字符串,如果我遇到任何粗体字,请在其周围添加标签。
I know you can read a cells contents character by character but not its formatting.
我知道您可以逐个字符读取单元格内容,但不能读取其格式。
Any help would be greatly appreciated!
任何帮助将不胜感激!
回答by Siddharth Rout
Here is a way that you can use to check if the cell has
这是一种您可以用来检查单元格是否具有的方法
- Mixed characters which are bold. In this case it will return
NULL
- All characters are bold. In this case it will return
TRUE
- None of the characters are bold. In this case it will return
FALSE
- 粗体的混合字符。在这种情况下它会返回
NULL
- 所有字符都是粗体。在这种情况下它会返回
TRUE
- 没有一个字符是粗体的。在这种情况下它会返回
FALSE
Example
例子
Sub Sample()
Debug.Print Range("A1").Font.Bold
Debug.Print Range("A2").Font.Bold
Debug.Print Range("A3").Font.Bold
End Sub
To check if a cell has any bold character you can use this function as well (Either from VBA or Worksheet)
要检查单元格是否有任何粗体字符,您也可以使用此功能(来自 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
Screenshot
截屏
And you can use .Characters().Font.FontStyle
to check if each character is bold or not. Use the above Range A1
example.
您可以.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
Screeenshot
截图
Modified Code
修改代码
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
Screenshot
截屏