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.
Contents
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:
After:
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:
The user can select “Yes” and “No” from the drop down lists:
The cell under the drop down list turns bold:
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 :
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:
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:
- VBA Excel, Italic Font
- VBA, Excel Font Formatting
- VBA, Excel Alignment
- Excel VBA, Formatting Cells and Ranges Using the Macro Recorder
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