Excel VBA, Italic Font

In this article I will explain how you can use VBA to change the italic font property of cells and ranges. I have also provided an example on how to check if a cells has the italic 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 italic:

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

or

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

Before:

Excel, VBA, Normal Font

After:

Excel, VBA, Italic Font

The following line of code removes the italic font property:

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

or

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


Example 1, Set Italic 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 italic. If the user selects “No” the italic 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 italic:

Excel VBA Italic Font Style
More italic font:

Excel VBA, More Italic 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 italic
        Range(Cells(2, i), Cells(2, i)).Font.Italic = True
    Else
        'removes the italic font property
        Range(Cells(2, i), Cells(2, i)).Font.Italic = False
    End If
Next i
End Sub

The line below changes the font format to italic:

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

The line below removes the italic font format:

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


Example 2, Find Cells With Italic 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 italic font style the cell above in row 1 will be colored green :

Excel VBA, Italic Cells

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

Excel VBA, Italic Cells Modified
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.Italic = 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 italic:

If Range(Cells(2, i), Cells(2, i)).Font.Italic = 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 *