Click to Rate and Give Feedback
TechNet
TechNet Library

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (4319)
How to: Refer to Named Ranges

Ranges are easier to identify by name than by A1 notation. To name a selected range, click the name box at the left end of the formula bar, type a name, and then press ENTER.

Referring to a Named Range

The following example refers to the range named "MyRange" in the workbook named "MyBook.xls."

Sub FormatRange()
    Range("MyBook.xls!MyRange").Font.Italic = True
End Sub

The following example refers to the worksheet-specific range named "Sheet1!Sales" in the workbook named "Report.xls."

Sub FormatSales()
    Range("[Report.xls]Sheet1!Sales").BorderAround Weight:=xlthin
End Sub

To select a named range, use the GoTo method, which activates the workbook and the worksheet and then selects the range.

Sub ClearRange()
    Application.Goto Reference:="MyBook.xls!MyRange"
    Selection.ClearContents
End Sub

The following example shows how the same procedure would be written for the active workbook.

Sub ClearRange()
    Application.Goto Reference:="MyRange"
    Selection.ClearContents
End Sub

Looping Through Cells in a Named Range

The following example loops through each cell in a named range by using a For Each...Next loop. If the value of any cell in the range exceeds the value of limit, the cell color is changed to yellow.

Sub ApplyColor()
    Const Limit As Integer = 25
    For Each c In Range("MyRange")
        If c.Value > Limit Then
            c.Interior.ColorIndex = 27
        End If
    Next c
End Sub



Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Formatting Cell Ranges using PowerShell      Thomas Lee   |   Edit   |   Show History
# get-excelrange.ps1
# opens an existing workbook in Excel 2007, using PowerShell
# and re-formats two ranges
# Thomas Lee - tfl@psp.co.uk

#

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


#
#
# Make Excel visible
#$excel.visible = $true
#

# Open a workbook
$workbook = $excel.workbooks.open("D:\foo\xlsx1.xlsx"
#


# Get sheet1
$sheet1 = $workbook.worksheets | where {$_.name -eq "sheet1"}

#

# Make A1-B1 bold
$range = $sheet1.range("A1:B1")
$range.font.bold = "true"
#

# Make a2:b2 Italic
$range1 = $sheet1.range("a2:b2")
$range1.font.italic = "true"
#


# Give the defined range (range name) a new font and size
$range2 = $sheet1.range("table1")
$range2.font.size=24
$range2.font.name="Airal"

Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker