Font.Italic property not working in Excel 2013

44 Views Asked by At

I'm trying to set a cell's font to being Italic, and all VBA references I've seen have told me to do it
cell.Font.Italic = True as so:

Function Test(testcell As Range)
    testcell.Font.Italic = True
    Test = testcell.Font.Italic
End Function

But unless the passed-in cell is already Italic the function always returns FALSE with no change to the cell in question.

I've tried using the FontStyle property and even hard coding the cell value, but the cell refuses to change.

Function Test(testcell As Range)
    Range("J95").Font.FontStyle = "Italic"
    Test = testcell.Font.Italic
End Function

The only way to get the function to return TRUE is to manually set the cell to Italic from the Ribbon, but obviously, that's not what I want.

1

There are 1 best solutions below

0
MGonet On

It's not that simple. UDFs called indirectly with the Evaluate method can do much more than regular UDFs, but they also don't have the full capabilities of macros. Among other things, they can't set attributes for Italic or Bold fonts.
In this case, use another workaround to run the macro. This can be done, for example, by using the Calculate event.
In the standard module, you need to put the code:

Public italcell As Range
Function CTest(testcell As Range)
    Set italcell = testcell
    CTest = True
End Function

And in the worksheet module, the Calculate event handler:

Private Sub Worksheet_Calculate()
   If Not italcell Is Nothing Then _
      italcell.Font.Italic = True
End Sub