Range.BorderAround Method
Range.BorderAround Method
Adds a border to a range and sets the Color, LineStyle, and Weight properties for the new border. Variant.

Syntax

expression.BorderAround(LineStyle, Weight, ColorIndex, Color)

expression   A variable that represents a Range object.

Parameters

NameRequired/OptionalData TypeDescription
LineStyleOptionalVariantOne of the constants of XlLineStyle specifying the line style for the border.
WeightOptionalXlBorderWeightThe border weight.
ColorIndexOptionalXlColorIndexThe border color, as an index into the current color palette or as a XlColorIndex constant.
ColorOptionalVariantThe border color, as an RGB value.

Return Value
Variant

Remarks

You must specify either ColorIndex or Color, but not both.

You can specify either LineStyle or Weight, but not both. If you don't specify either argument, Microsoft Excel uses the default line style and weight.

This method outlines the entire range without filling it in. To set the borders of all the cells, you must set the Color , LineStyle , and Weight properties for the Borders collection. To clear the border, you must set the LineStyle property to xlLineStyleNone for all the cells in the range.

Example

This example adds a thick red border around the range A1:D4 on Sheet1.

Visual Basic for Applications
Worksheets("Sheet1").Range("A1:D4").BorderAround _
        ColorIndex:=3, Weight:=xlThick



Community Content

Formatting cell borders using Excel
Added by:Thomas Lee
  

# format-borders.ps1
# Creates a new workbook (with just one sheet), in Excel 2007
# Then we show different cell borders
# Thomas Lee - tfl@psp.co.uk


# define enum for line type

$XlLineStyle = @{}
$xllinestyle += @{"xlContinuous" = "1"} # Continuous line.
$xllinestyle += @{"xlDash" = '-4115'} # Dashed line.
$xllinestyle += @{"xlDashDot" = ' 4'} # Alternating dashes and dots.
$xllinestyle += @{"xlDashDotDot" = '5'} # Dash followed by two dots.
$xllinestyle += @{"xlDot" = '-4118'} # Dotted line.
$xllinestyle += @{"xlDouble" = ' -4119'} # Double line.
$xllinestyle += @{"xlLineStyleNone" = ' -4142'} # No line.
$xllinestyle += @{"xlSlantDashDot" = ' 13'} # Slanted dashes.


# Create Excel object
$excel = new-object -comobject Excel.Application

# make Excel visible
$excel.visible = $true


# create a new workbook
$workbook = $excel.workbooks.add()

# default workbook has three sheets, remove 2
# nb: these need to be confirmed in Excel. :-(
$S2 = $workbook.sheets | where {$_.name -eq "Sheet2"}
$s3 = $workbook.sheets | where {$_.name -eq "Sheet3"}
$s3.delete()
$s2.delete()


# get sheet and update sheet name
$s1 = $workbook.sheets | where {$_.name -eq 'Sheet1'}
$s1.name = "PowerShell Sample - Borders"

$s1.range("a1:a1").cells=10
$s1.range("a2:a2").cells=10
$s1.range("a3:A3").cells.formula = "=sum(A1,A2)"

$s1.range("b1:B1").cells=21
$s1.range("b2:b2").cells=21
$s1.range("b3:b3").cells.formula = "=sum(B1,B2)"

$s1.range("D1:D1").cells=2
$s1.range("D2:D2").cells=2
$s1.range("D3:D3").cells.formula = "=sum(d1,d2)"


#Give Cell ranges nice names
$s1.range("A1:A3").name="Dimsum1"

$s1.range("B1:B3").name="Dimsum2"

$s1.range("d1:d3").name="Dimsum3"


# Lastly, set the different borders
$s1.range("dimsum1").borderaround($xllinestyle.xlContinuous)
$s1.range("dimsum2").borderaround($xllinestyle.xlDashDot)
$s1.range("dimsum3").borderaround($xllinestyle.xldashdotdot)

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View