Sunday, 22 February 2015

Excel : Changing Font and Size Conditionally

Is there a way to use Excel's conditional formatting capabilities to change the font used in a cell or to change the font size in a cell. The short answer is no, that can't be done—at least not with conditional formatting. 


You can, however, use a macro to examine cell contents and make changes in the appearance of a cell. Consider the following macro, which examines any cells you have selected when you run the macro. If any of the cells have a length of more than two characters or a value of more than 20, then the cell's font is changed.

Below are the steps to follow

1) Open Excel
2) Click on Developer Tab
3) Click On Macro
4) Write below Macro Code

Sub Check_Conditional_Formattinf()
    Dim rCell As Range
    For Each rCell In Selection.Cells
        If Len(rCell.Text) > 2 Or _
          Val(rCell.Value) > 20 Then
            rCell.Font.Name = "Cambria"
            rCell.Font.Size = 30
        Else
            rCell.Font.Name = "Arial"
            rCell.Font.Size = 12
        End If
    Next
End Sub

5) Open Excel
6) Write from 1 to 100 in Cell A1 to A100
7) Select Cells
8) Run Macro

and here will be the result...


Keep Learning
Sanjay Bakshi
mesanjaybakshi@gmail.com

No comments:

Post a Comment