Cómo: Aplicar estilos a los rangos de los libros

Puede aplicar estilos con nombre a distintas áreas de los libros. Excel proporciona muchos estilos predefinidos.

Se aplica a: la información de este tema se aplica a los proyectos de nivel de documento y los proyectos de nivel de aplicación para Excel 2007 y Excel 2010. Para obtener más información, vea Características disponibles por aplicación y tipo de proyecto de Office.

El cuadro de diálogo Formato de celdas muestra todas las opciones que puede utilizar para dar formato a las celdas; estas opciones también están disponibles en el código. Para mostrar este cuadro de diálogo en Excel, haga clic en Celdas en el menú Formato.

Para aplicar un estilo a un rango con nombre en una personalización en el nivel del documento

  1. Cree un nuevo estilo y establezca sus atributos.

    Dim style As Excel.Style = Globals.ThisWorkbook.Styles.Add("NewStyle")
    
    style.Font.Name = "Verdana"
    style.Font.Size = 12
    style.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
    style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray)
    style.Interior.Pattern = Excel.XlPattern.xlPatternSolid
    
    Excel.Style style = Globals.ThisWorkbook.Styles.Add("NewStyle", missing);
    
    style.Font.Name = "Verdana";
    style.Font.Size = 12;
    style.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray);
    style.Interior.Pattern = Excel.XlPattern.xlPatternSolid;
    
  2. Cree un control NamedRange, asígnele texto y, a continuación, aplique el nuevo estilo. Este código debe colocarse en una clase Sheet, no en la clase ThisWorkbook.

    Dim rangeStyles As Microsoft.Office.Tools.Excel.NamedRange = _
        Me.Controls.AddNamedRange(Me.Range("A1"), "rangeStyles")
    
    rangeStyles.Value2 = "'Style Test"
    rangeStyles.Style = "NewStyle"
    rangeStyles.Columns.AutoFit()
    
    Microsoft.Office.Tools.Excel.NamedRange rangeStyles =
        this.Controls.AddNamedRange(this.Range["A1", missing], "rangeStyles");
    
    rangeStyles.Value2 = "'Style Test";
    rangeStyles.Style = "NewStyle";
    rangeStyles.Columns.AutoFit();
    

Para aplicar un estilo a un rango con nombre en un complemento en el nivel de la aplicación

  1. Cree un nuevo estilo y establezca sus atributos.

    Dim style As Excel.Style = Me.Application.ActiveWorkbook.Styles.Add("NewStyle")
    
    style.Font.Name = "Verdana"
    style.Font.Size = 12
    style.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
    style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray)
    style.Interior.Pattern = Excel.XlPattern.xlPatternSolid
    
    Excel.Style style = this.Application.ActiveWorkbook.Styles.Add("NewStyle", missing);
    
    style.Font.Name = "Verdana";
    style.Font.Size = 12;
    style.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray);
    style.Interior.Pattern = Excel.XlPattern.xlPatternSolid;
    
  2. Cree un Microsoft.Office.Interop.Excel.Range, asígnele texto y, a continuación, aplique el nuevo estilo.

    Dim rangeStyles As Excel.Range = Me.Application.Range("A1")
    
    rangeStyles.Value2 = "'Style Test"
    rangeStyles.Style = "NewStyle"
    rangeStyles.Columns.AutoFit()
    
    Excel.Range rangeStyles = this.Application.get_Range("A1", missing);
    
    rangeStyles.Value2 = "'Style Test";
    rangeStyles.Style = "NewStyle";
    rangeStyles.Columns.AutoFit();
    

Vea también

Tareas

Cómo: Borrar estilos de los rangos de los libros

Conceptos

Trabajar con rangos

NamedRange (Control)

Acceso global a objetos en los proyectos de Office

Parámetros opcionales en las soluciones de Office