Excel VBA, Bold Font

In this article I will explain how you can use VBA to change the bold font property of cells and ranges. I have also provided an example on how to check if a cells has the bold font property on or off.

Jump To:

You can download the code and file related to this article here.


Basics:

The following line of code makes the font in cell A1 bold:

Range("A1").Font.Bold = True

or

Range("A1").Font.FontStyle = "Bold"

Before:

Excel, VBA, Normal Font

After:

Excel, VBA, Bold Font

The following line of code removes the bold font property:

Range("A1").Font.Bold = False

or

Range("A1").Font.FontStyle = "Regular"


Example 1, Set Bold Property:

In this example there are a set of drop down lists in row 1. Each drop down list has the values “Yes” and “No”. For more information about creating drop down lists  in Excel please see Excel VBA Drop Down Lists. If the user selects “Yes” the text in the cell under the drop down list will become bold. If the user selects “No” the bold font style from the cell below the drop down list will be removed. The “Yes”, “No” values for the drop down lists are in sheet 2:

Excel, VBA, Drop Down List Values
The user can select “Yes” and “No” from the drop down lists:

Excel VBA, Select Yes
The cell under the drop down list turns bold:

Excel VBA, Bold Font
More bold font:

Excel VBA, More Bold Font
The program uses a worksheet_change event handler. The event handler executes when the user selects a value from the drop down lists:

'Executes when the user selects a new values from the drop down lists
Private Sub worksheet_change(ByVal target As Range)
Dim i As Integer
For i = 1 To 6
    'checks if the Yes property has been selected
    If Cells(1, i) = "Yes" Then
        'changes the font to bold
        Range(Cells(2, i), Cells(2, i)).Font.Bold = True
    Else
        'removes the bold font property
        Range(Cells(2, i), Cells(2, i)).Font.Bold = False
    End If
Next i
End Sub

The line below changes the font format to bold:

Range(Cells(2, i), Cells(2, i)).Font.Bold = True

The line below removes the bold font format:

Range(Cells(2, i), Cells(2, i)).Font.Bold = False


Example 2, Find Cells With Bold Font:

In this example when the user presses the Run button the program will loop through the cells in row 2. If the cell in row 2 has the bold font style the cell above in row 1 will be colored green :

Excel VBA, Get Bold FOnt

In the figure below the bold font style was removed from cell C2 and the font style in cell B2 was changed to bold. After pressing the run button:

Excel VBA, Get, Bold Font, Modify
The code below executes when the user presses the run button:

'executes when the user presses the run button
Private Sub btnRun_Click()
Dim i As Integer
'loops through row 2
For i = 1 To 6
    'checks if the font in the cell is bold
    If Range(Cells(2, i), Cells(2, i)).Font.Bold = True Then
        'changes the color of the cell in row 1 to green
        Range(Cells(1, i), Cells(1, i)).Interior.Color = 3394611
    Else
        'removes any previous color applied to the content of the cell in row 1
        Range(Cells(1, i), Cells(1, i)).Interior.Color = xlNone
    End If
Next i
End Sub

The If statement below checks if the font in the cell is bold:

If Range(Cells(2, i), Cells(2, i)).Font.Bold = True Then
...
End If

The line below changes the color of the cell in row 1 to green. The number 3394611 is a color code. It was found using the macro recorder. For more information about using the macro recorder please see Excel VBA, Formatting Cells and Ranges Using the Macro Recorder:

Range(Cells(1, i), Cells(1, i)).Interior.Color = 3394611

The line below removes any previous coloring applied to the cell in row 1:

Range(Cells(1, i), Cells(1, i)).Interior.Color = xlNone

You can download the code and file related to this article here.

See also:

If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website  www.software-solutions-online.com

Leave a Reply

Your email address will not be published. Required fields are marked *