CellFormat Object

Microsoft Excel Visual Basic

CellFormat Object

Application CellFormat
Multiple objects

Represents the search criteria for the cell format.

Using the CellFormat object

Use the FindFormat or ReplaceFormat properties of the Application object to return a CellFormat object.

With a CellFormat object, you can use the Borders,Font, orInterior properties of the CellFormat object, to define the search criteria for the cell format. The following example sets the search criteria for the interior of the cell format. In this scenario, the interior of cell A1 is set to yellow, which is then found and replaced with a green interior.

Sub ChangeCellFormat()

    ' Set the interior of cell A1 to yellow.
    Range("A1").Select
    Selection.Interior.ColorIndex = 36
    MsgBox "The cell format for cell A1 is a yellow interior."

    ' Set the CellFormat object to replace yellow with green.
    With Application
        .FindFormat.Interior.ColorIndex = 36
        .ReplaceFormat.Interior.ColorIndex = 35
    End With

    ' Find and replace cell A1's yellow interior with green.
    ActiveCell.Replace What:="", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=True, _
        ReplaceFormat:=True
    MsgBox "The cell format for cell A1 is replaced with a green interior."

End Sub