简体   繁体   中英

In an Excel User-Defined function, how can I determine which font is being used?

I have an Excel file with thousands of lines. Some cells are bold , others not. Usually I am able to determine which cells are bold and which are not using this UDF:

Function isBold(cellBold)
    Application.Volatile
    If cellBold.Font.Bold = True Then
        isBold = 1
    ElseIf cellBold.Font.Bold = False Then
    isBold = 0
    Else
        isBold = 0
    End If
End Function

This works well on cells that actually have bold applied to the cells EXCEPT that the data is imported and the original source alters the text from regular to bold by altering the font, not by applying bold to the cell.

For example, the regular font is 'Times New Roman' and the bold font is 'Times New Roman Bold'. Because of this, although on the page it looks as though the cell has had bold applied to it, the UDF above doesn't work.

How can I alter the UDF so that the font is identified?

This code finds out if the font name contains "Bold" string and sets your boolean to 1 if so:

Function isBold(cellBold)
     Application.Volatile
     pos = InStr(cellBold.Font.Name, "Bold")
     If pos > 0 Then
        isBold = 1
     End If  
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM