Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 210,483 times

Contents

Related Categories

VBA in Excel - Cell Formatting

Cell Formatting

To apply formatting to a cell, you use the Interior object. For example,

ActiveCell.Interior.Pattern = xlSolid

sets the active cells background pattern to a solid. Its other main properties are shown below:

Property Name
Color
ColorIndex
Pattern
PatternColor
PatternColorIndex

As explained in the last section, using ColorIndex you can specify a color from the standard palette, or using the Color property you can specify an exact colour.

The possible values for the Pattern property are shown below:

xlPatternAutomatic

xlPatternChecker

xlPatternCrissCross

xlPatternDown

xlPatternGray16

xlPatternGray25

xlPatternGray50

xlPatternGray75

xlPatternGray8

xlPatternGrid

xlPatternHorizontal

xlPatternLightDown

xlPatternLightHorizontal

xlPatternLightUp

xlPatternLightVertical

xlPatternNone

xlPatternSemiGray75

xlPatternSolid

xlPatternUp

xlPatternVertical

For example, the following code sets the background colour to blue, the pattern to red horizontal lines:

With ActiveCell.Interior
    .Color = RGB(0,0,255)
    .Pattern = xlPatternHorizontal
    .PatternColor = RGB(255,0,0)
End With

You can also set a cells border using the Borders object and BordersAround object. With these, you can set the following:

Property Name
Color
ColorIndex
Item
LineStyle
Value
Weight

The Item property allows you to set a specific border. You can use the BordersAround object to set the 'perimeter' border for a selection of cells

The following code sets the active cell's border to red, and makes the top border thicker:

ActiveCell.Borders.Color = RGB(255,0,0)
ActiveCell.Borders.Item(xlEdgeTop).Weight = xlThick

For more detailed help, type Range("A2").Borders (or another valid range... not ActiveCell as VB doesn't know what that is until the code is being run) and press F1

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [35] VBA in Excel

    Posted by MartinMudge on 28 Aug 2007

    Hi


    What i am trying to do is use a formula to grap cells a specific distance apart, and then use autofill to copy these cells.  Currently, I am ...

  • Re: [35] VBA in Excel

    Posted by erniede on 10 Aug 2007

    This might not be the place to ask this question, but here goes...


    I'd like to put a macro in an excel worksheet that limits the time someone can use the sheet. that is, I have a "demo" of ...

  • deleting unwanted rows

    Posted by charuambekar on 08 Feb 2006


    You need to first write a code to select the entire range, then


    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.EntireRow.Delete



    Regards,

    Charu

  • .Borders

    Posted by djlysuc on 26 Nov 2005

    I am trying to use VB in excel 2003 to check if a cell has a border. I tried the following but it does not work. Can anyone explain how I should be doing this?

    [code]
    ActiveCell.Borders(xlEdgeLeft...

  • Try something different

    Posted by richardbarnwell on 14 Nov 2005

    Why dont you try using a For loop instead. Something like:

    x = o

    For x to 100

    // What ever you want to do

    Next x

    Makes it very easy.