Udostępnij za pośrednictwem


How to: Apply Color to Excel Ranges

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Excel 2003

  • Excel 2007

For more information, see Features Available by Application and Project Type.

To apply a color to text within a range of cells, use a NamedRange control or a native Excel range object.

Using a NamedRange Control

This example is for document-level customizations.

To apply color to a NamedRange control

  1. Create a NamedRange control at cell A1.

    Dim rng As Microsoft.Office.Tools.Excel.NamedRange = _
        Me.Controls.AddNamedRange(Me.Range("A1"), "NamedRange1")
    
    Microsoft.Office.Tools.Excel.NamedRange rng =
        this.Controls.AddNamedRange(this.Range["A1", missing], "NamedRange1");
    
  2. Set the color of the text in the NamedRange control.

    rng.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
    
    rng.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    

Using Native Excel Ranges

To apply color to a native Excel range object

  • Create a range at cell A1 and then set the color of the text.

    Dim rng2 As Excel.Range = Me.Application.Range("A1")
    rng2.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
    
    Excel.Range rng2 = this.Application.get_Range("A1", missing);
    rng2.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    

See Also

Tasks

How to: Apply Styles to Ranges in Workbooks

How to: Refer to Worksheet Ranges in Code

Concepts

Working with Ranges

NamedRange Control

The Variable missing and Optional Parameters in Office Solutions

Other Resources

Excel Host Controls