2013-04-25 35 views
7

我從工作表中的文本列表生成XML,但無法確定如何檢查當前單元格是否包含粗體字。我需要做的是檢查列A中的每個單元格,將文本讀入一個字符串,如果我點擊任何粗體字,請在其周圍添加標籤。Vba檢查單元格是否部分粗體

我知道你可以按字符讀取單元格內容,但不能格式化。

任何幫助將不勝感激!

回答

8

這是一種方式,你可以用它來檢查電池具有

  1. 混合字符,這是大膽的。在這種情況下,它將返回NULL
  2. 所有字符都是粗體。在這種情況下,它將返回TRUE
  3. 這些字符都不是粗體。在這種情況下,它會返回FALSE

enter image description here

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

enter image description here

要檢查電池具有您可以使用此功能以及任何大膽的字符( 來自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 

截圖

enter image description here enter image description here

你也可以使用.Characters().Font.FontStyle檢查,如果每個字符是大膽與否。使用上述範圍A1的例子。

Sub Sample() 
    For i = 1 To Len(Range("A1").Value) 
     Debug.Print Range("A1").Characters(i, 1).Font.FontStyle 
    Next i 
End Sub 

Screeenshot

enter image description here

改進型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 

截圖

enter image description here

+0

Sid,我得到你的'Character'代碼,但是我不清楚'FinBoldCharacters'在做什麼或者它是如何調用的? – brettdj 2013-04-25 09:19:50

相關問題