引用命名区域

用名称比用 A1 样式记号更容易标识单元格区域。 若要命名选定的单元格区域,请单击编辑栏左端的名称框,键入名称,再按 Enter。

注意 有两种类型的命名区域:工作簿命名范围和特定于 WorkSHEET 的命名范围。

工作簿命名区域

工作簿命名区域引用工作簿任何位置中的特定区域(全局适用)。

如何创建工作簿命名区域:

如上所述,它通常是通过在公式栏最左边的名称框中输入名称创建的。 请注意,名称中不允许使用空格。

工作表特定命名区域

工作表特定命名区域引用特定工作表中的一个区域,它并不全局适用于工作簿内的所有工作表。 仅通过同一工作表中的名称来引用此命名区域,但在另一个工作表中,必须使用工作表名称,包括“!”区域名称 (示例:区域“Name”“=Sheet1!Name“) 。

优点是你可以使用 VBA 代码为相同区域生成具有相同名称的新工作表,而不会出现指明名称已被占用的错误。

如何创建工作表特定命名区域:

  1. 选择要命名的区域。
  2. 单击窗口顶部 Excel 功能区上的“公式”选项卡。
  3. 单击“公式”选项卡中的“定义名称”按钮。
  4. 在“新建名称”对话框中的“范围”字段下选择要定义的区域所在的特定工作表(即“Sheet1”)- 这将使名称特定于此工作表。 如果选择“工作簿”,则名称将是工作簿名称)。

WorkSHEET 特定命名范围的示例:要命名的选定范围是 A1:A10

所选的区域名称为“name”,在同一工作表内,只需通过在单元格中输入“=name”来引用指定名称,从其他工作表中,则需要在单元格中包括工作表名称(“=Sheet1!name”)来引用 工作表特定区域。

引用命名区域

以下示例引用名为“MyBook.xls”的工作簿中名为“MyRange”的区域。

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

以下示例引用名为“Report.xls”的工作簿中特定于工作表的区域“Sheet1!Sales”。

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

要选定命名区域,请使用 GoTo 方法,该方法将激活工作簿和工作表,然后选定该区域。

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

以下示例演示如何为活动工作簿编写相同的过程。

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

示例代码提供者:Dennis Wallentin,VSTO & .NET & Excel

此示例使用命名区域作为公式来进行数据验证。 此示例要求验证数据位于 Sheet2 上的区域 A2:A100 中。 此验证数据用于验证在 Sheet1 上的区域 D2:D10 中输入的数据。

Sub Add_Data_Validation_From_Other_Worksheet()
'The current Excel workbook and worksheet, a range to define the data to be validated, and the target range
'to place the data in.
Dim wbBook As Workbook
Dim wsTarget As Worksheet
Dim wsSource As Worksheet
Dim rnTarget As Range
Dim rnSource As Range

'Initialize the Excel objects and delete any artifacts from the last time the macro was run.
Set wbBook = ThisWorkbook
With wbBook
    Set wsSource = .Worksheets("Sheet2")
    Set wsTarget = .Worksheets("Sheet1")
    On Error Resume Next
    .Names("Source").Delete
    On Error GoTo 0
End With

'On the source worksheet, create a range in column A of up to 98 cells long, and name it "Source".
With wsSource
    .Range(.Range("A2"), .Range("A100").End(xlUp)).Name = "Source"
End With

'On the target worksheet, create a range 8 cells long in column D.
Set rnTarget = wsTarget.Range("D2:D10")

'Clear out any artifacts from previous macro runs, then set up the target range with the validation data.
With rnTarget
    .ClearContents
    With .Validation
        .Delete
        .Add Type:=xlValidateList, _
             AlertStyle:=xlValidAlertStop, _
             Formula1:="=Source"
        
'Set up the Error dialog with the appropriate title and message
        .ErrorTitle = "Value Error"
        .ErrorMessage = "You can only choose from the list."
    End With
End With

End Sub

在命名区域中的各个单元格中循环

以下示例使用 For Each...Next 循环语句在命名区域中的每一个单元格中循环。 如果该区域中的任一单元格的值超过 Limit 的值,则该单元格的颜色会变为黄色。

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

关于参与者

Dennis Wallentin 是 VSTO & .NET & Excel 的作者,该博客专注于适用于 Excel 和 Excel Services 的 .NET Framework 解决方案。 Dennis 已经从事 Excel 解决方案开发超过 20 年,同时也是“专业 Excel 开发:使用 Microsoft Excel、VBA 和 .NET 开发应用程序的权威指南(第 2 版)”的合著者。

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。