Add Method (Styles Collection)

Applies To

Styles collection object.

Description

Creates a new style and adds it to the list of styles that are available for the current workbook. Returns a Style object.

Syntax

expression.Add(Name, BasedOn)

expression Required. An expression that returns a Styles object.

Name Required String. The new style name.

BasedOn Optional Variant. A Range object that refers to a cell that's used as the basis for the new style. If this argument is omitted, the newly created style is based on the Normal style.

Remarks

If a style with the specified name already exists, this method redefines the existing style based on the cell specified in BasedOn. The following example redefines the Normal style based on the active cell.

ActiveWorkbook.Styles.Add Name := "Normal", _
    BasedOn := ActiveCell
Example

This example defines a new style based on cell A1 on Sheet1.

Worksheets("Sheet1").Activate
ActiveWorkbook.Styles.Add Name:="myNewStyle", _
    BasedOn:=ActiveSheet.Range("A1")
This example defines a new style that includes only font properties.

With ActiveWorkbook.Styles.Add(Name:="theNewStyle")
    .IncludeNumber = False
    .IncludeFont = True
    .IncludeAlignment = False
    .IncludeBorder = False
    .IncludePatterns = False
    .IncludeProtection = False
    .Font.Name = "Arial"
    .Font.Size = 18
End With