Export (0) Print
Expand All

How to: Programmatically Refer to Worksheet Ranges in Code

You use a similar process to refer to the contents of a NamedRange control or a native Excel range object.

Applies to: The information in this topic applies to document-level projects and application-level projects for Excel 2013 and Excel 2010. For more information, see Features Available by Office Application and Project Type.

The following example adds a NamedRange to a worksheet and then adds text to the cell in the range.

To refer to a NamedRange control

  • Assign a string to the Value2 property of the Microsoft.Office.Tools.Excel.NamedRange control. This code must be placed in a sheet class, not in the ThisWorkbook class.

    Dim NamedRange1 As Microsoft.Office.Tools.Excel.NamedRange = _
        Me.Controls.AddNamedRange(Me.Range("A1"), "NamedRange1")
    
    NamedRange1.Value2 = "Range value"
    

The following example adds a native Excel range to a worksheet and then adds text to the cell in the range.

To refer to a native range object

  • Assign a string to the Value2 property of the range.

    Dim rng As Excel.Range = Me.Application.Range("A1")
    
    rng.Value2 = "Range value"
    
Show:
© 2014 Microsoft