名前付き範囲の参照 | Microsoft Learn

記事では、Excelの名前付き範囲について説明しています。名前付き範囲には、ワークブック全体で参照できる「Workbook Named Range」と特定のワークシートに限定される「Worksheet Specific Named Range」の2種類があります。範囲には名前を付けることができ、それを使ってVBAコードで新しいシートを生成する際にエラーを回避できます。また、名前付き範囲はデータの検証や条件付き書式に利用され、VBAを用いて効率的に操作できます。

Excelにおける名前付き範囲の活用

Excelでは、範囲をA1形式で扱うことが一般的ですが、名前を付けることで範囲を特定する方が容易になります。選択範囲に名前を付けるには、数式バーの左端にある名前ボックスをクリックし、名前を入力してENTERを押します。

名前付き範囲の種類

名前付き範囲には2つのタイプがあります:

  1. ブック全体の名前付き範囲(Workbook Named Range)
  2. ワークシート特有の名前付き範囲(Worksheet Specific Named Range)

ブック全体の名前付き範囲

ブック全体の名前付き範囲は、ワークブック内のどこからでも特定の範囲を参照できます。この範囲は全体に適用されます。

ブック全体の名前付き範囲の作成方法

通常、数式バーの左端にある名前ボックスに名前を入力することで作成します。名前にはスペースを含めることはできません。

ワークシート特有の名前付き範囲

ワークシート特有の名前付き範囲は、特定のワークシート内の範囲を参照し、ワークブック内の全てのワークシートに対してはグローバルではありません。この範囲を他のワークシートから参照する場合、ワークシート名を含める必要があります(例:=Sheet1!Nameのように)。

この利点として、VBAコードを使用して同じ名前の新しいシートを生成する際に、すでに存在する名前と衝突することがなくなります。

ワークシート特有の名前付き範囲の作成方法

  1. 名前を付けたい範囲を選択します。
  2. Excelリボンの「数式」タブをクリックします。
  3. 「名前の定義」ボタンをクリックします。
  4. 「新しい名前」ダイアログボックスで、「スコープ」のフィールドから範囲が存在する特定のワークシートを選択します(例:「Sheet1」)。これにより、そのワークシートに対して特有の名前が作成されます。

名前付き範囲への参照

以下の例では、ワークブック「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

セルをループして色を変更

以下の例では、名前付き範囲内の各セルをループし、値が25を超える場合はそのセルの色を黄色に変更します。

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

編集者について

デニス・ウォレンティンは「VSTO & .NET & Excel」というブログの著者であり、ExcelとExcel Servicesのための.NET Frameworkソリューションに特化しています。彼は20年以上にわたりExcelソリューションを開発しており、「Professional Excel Development」の共同著者でもあります。

サポートとフィードバック

Office VBAやこのドキュメントに関する質問やフィードバックがある場合は、Office VBA サポートとフィードバックを参照してください。

Excelでの名前付き範囲を活用することで、作業の効率を大きく向上させることができるため、ぜひ試してみてください。

————-

Refer to Named Ranges | Microsoft Learn

Source link

The article discusses the concept of named ranges in Excel, highlighting their benefits and usage, particularly in the context of VBA (Visual Basic for Applications) programming. Named ranges make it easier to reference specific cells or groups of cells in Excel compared to standard A1 notation.

Key Points:

  1. Types of Named Ranges:

    • Workbook Named Range: This type of range can be accessed from any worksheet within the workbook and applies globally.
    • Worksheet Specific Named Range: This range is limited to the specific worksheet it is defined in. When referring to it from another worksheet, it requires the worksheet name prefixed (e.g., =Sheet1!Name).
  2. Creating Named Ranges:

    • Workbook Named Range: Can be created by typing the name in the name box (located to the left of the formula bar) with no spaces.
    • Worksheet Specific Named Range: Requires the user to select the range, then navigate to the "Formulas" tab, click "Define Name," and choose the specific worksheet scope.
  3. Using Named Ranges:

    • Examples are provided for how to refer to named ranges within VBA code, including formatting ranges and applying data validation.
    • There are methods to loop through cells in a named range, which can facilitate operations like conditional formatting.
  4. VBA Examples: The article includes sample VBA code that shows various ways to interact with named ranges, including looping through each cell, applying color based on conditions, and creating data validation from a named range.

  5. Contributor’s Background: The article is contributed by Dennis Wallentin, an experienced developer in Excel solutions and author of a related blog.

Overall, the article serves as a comprehensive guide for users looking to effectively use and manage named ranges in Excel, particularly for those who wish to automate tasks using VBA.

関連記事

コメント

この記事へのコメントはありません。