Tuesday, March 3, 2009

Excel VBA: Font Formatting

When we working in Excel, if we want to changes font properties we may use Font tab in the Format Cells dialog box. In Excel VBA, we control the font's appearance by the Font object. The Font object have several properties that correspond to various aspects of the font's appearance. Here are several list of font's properties:

Property Description
Name
The name of the font.
Bold True if the font is bold. Read/write Variant.
Italic True if the font style is italic. Read/write Boolean.
Underline Returns or sets the type of underline applied to the font.
Can be set to xlUnderlineStyleNone, xlUnderlineStyleSingle,
xlUnderlineStyleDouble, xlUnderlineStyleSingleAccounting,
xlUnderlineStyleDoubleAccounting. Read/write Variant.
Size Returns or sets the size of the font. Read/write Variant.
Subscript True if the font is formatted as subscript. False by default. Read/write Variant.
Superscript True if the font is formatted as superscript; False by default. Read/write Variant.
Strikethrough True if the font is struck through with a horizontal line. Read/write Boolean.

This example sets font name of range A1:B4 to Tahoma in ActiveSheet in ActiveWorkbook:

ActiveWorkbook.ActiveSheet.Range("A1:B4").Font.Name = "Tahoma"

The Color property uses an RGB value, which identifies a color in terms of its red, green,
and blue components. To set this property, use the RGB function:

RGB(r, g, b)

The next example sets font color of range B4 to Blue in ActiveSheet in ActiveWorkbook:

ActiveWorkbook.ActiveSheet.Range("A1:B4").Font.Color = RGB(0, 0, 255)

We can also use predefined constants to sets font color, they are vbBlack, vbRed, vbGreen, vbYellow, vbBlue, vbMagenta, vbCyan, and vbWhite.

This example do exactly as previous example:

ActiveWorkbook.ActiveSheet.Range("A1:B4").Font.Color = vbBlue



Related Post:
---
If you like posts in this blog, you can to support me :)

No comments:

Post a Comment