How to: Resize NamedRange controls
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
You can set the size of a NamedRange control when you add it to a Microsoft Office Excel document; however, you might want to resize it at a later time.
Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Excel. For more information, see Features available by Office application and project type.
You can resize a named range at design time or at run time in document-level projects. You can also resize named ranges at run time in application-level VSTO Add-ins.
This topic describes the following tasks:
Resize NamedRange controls at run time in a document-level project
Resize NamedRange controls at run time in a VSTO Add-in project
Resize NamedRange controls at design time
You can resize a named range by redefining its size in the Define Name dialog box.
To resize a named range by using the Define Name dialog box
Right-click a NamedRange control.
Click Manage Named Ranges on the shortcut menu.
The Define Name dialog box appears.
Select the named range you want to resize.
Clear the Refers to box.
Select the cells you want to use to define the size of the named range.
Click OK.
Resize NamedRange controls at run time in a document-level project
You can resize a named range programmatically by using the RefersTo property.
Note
In the Properties window, the RefersTo property is marked as read-only.
To resize a named range programmatically
Create a NamedRange control on cell A1 of
Sheet1
.Microsoft.Office.Tools.Excel.NamedRange NamedRange1 = this.Controls.AddNamedRange(this.Range["A1"], "NamedRange1");
Dim NamedRange1 As Microsoft.Office.Tools.Excel.NamedRange = _ Me.Controls.AddNamedRange(Me.Range("A1"), "NamedRange1")
Resize the named range to include cell B1.
NamedRange1.RefersTo = "=Sheet1!$A$1:$B$1";
NamedRange1.RefersTo = "=Sheet1!$A$1:$B$1"
Resize NamedRange controls at run time in a VSTO Add-in project
You can resize a NamedRange control on any open worksheet at run time. For more information about how to add a NamedRange control to a worksheet by using a VSTO Add-in, see How to: Add NamedRange controls to worksheets.
To resize a named range programmatically
Create a NamedRange control on cell A1 of
Sheet1
.Worksheet worksheet = Globals.Factory.GetVstoObject(Application.ActiveSheet); Microsoft.Office.Tools.Excel.NamedRange namedRange1 = worksheet.Controls.AddNamedRange( worksheet.Range["A1"], "MyNamedRange");
Dim NativeWorksheet As Microsoft.Office.Interop.Excel.Worksheet = _ Application.ActiveSheet Dim worksheet As Microsoft.Office.Tools.Excel.Worksheet = _ Globals.Factory.GetVstoObject(NativeWorksheet) Dim namedRange1 As Microsoft.Office.Tools.Excel.NamedRange namedRange1 = worksheet.Controls.AddNamedRange(worksheet.Range("A1"), "MyNamedRange")
Resize the named range to include cell B1.
namedRange1.RefersTo = "=Sheet1!$A$1:$B$1";
namedRange1.RefersTo = "=Sheet1!$A$1:$B$1"
See also
- Extend Word documents and Excel workbooks in VSTO Add-ins at run time
- Add controls to Office documents at run time
- Controls on Office documents
- Host items and host controls overview
- Automate Excel by using extended objects
- NamedRange control
- How to: Add NamedRange controls to worksheets
- How to: Resize Bookmark controls
- How to: Resize ListObject controls