アプリケーション.選択プロパティ (Excel) | Microsoft Learn

アクティブなワークシートで現在選択されているオブジェクトを返すApplicationオブジェクトのプロパティです。オブジェクトが選択されていないときはNothingを返します。選択を設定するにはSelectメソッドを使用し、TypeName関数で選択されたオブジェクトの種類を確認します。Selectionプロパティは、選択されていない場合Nothingを返します。例として、Sheet1の選択をクリアしたり、選択されたオブジェクトのタイプを表示したりする方法が示されています。

Excel VBAにおけるSelectionプロパティの使用

Excel VBAでは、アクティブなワークシート上で現在選択されているオブジェクトを取得するために、Selectionプロパティを使用します。このプロパティは、選択されたオブジェクトがない場合にはNothingを返します。オブジェクトを選択するには、Selectメソッドを使用し、選択されているオブジェクトの種類を確認するには、TypeName関数を使用します。

構文

vb
expression.Selection

  • expression: Applicationオブジェクトを表す変数。

返されるオブジェクトの種類は、現在の選択に依存します。例えば、セルが選択されている場合、SelectionプロパティはRangeオブジェクトを返します。何も選択されていない場合、SelectionプロパティはNothingを返します。このプロパティをオブジェクト修飾子なしで使用することは、Application.Selectionを使用するのと同等です。

例1: シート1の選択をクリアする

この例では、Sheet1の選択をクリアします(選択がセルの範囲であることを前提としています)。

vb
Worksheets(“Sheet1”).Activate
Selection.Clear

例2: 選択されたオブジェクトのタイプを表示する

この例では、選択されたオブジェクトのVisual Basicのオブジェクトタイプを表示します。

vb
Worksheets(“Sheet1”).Activate
MsgBox “The selection object type is ” & TypeName(Selection)

例3: 現在の選択に関する情報を表示する

この例では、現在の選択に関する情報を表示します。

vb
Sub TestSelection()
Dim str As String
Select Case TypeName(Selection)
Case “Nothing”
str = “No selection made.”
Case “Range”
str = “You selected the range: ” & Selection.Address
Case “Picture”
str = “You selected a picture.”
Case Else
str = “You selected a ” & TypeName(Selection) & “.”
End Select
MsgBox str
End Sub

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

Office VBAやこの文書に関する質問やフィードバックがある場合は、Office VBA support and feedbackをご覧ください。ここでは、サポートを受けたりフィードバックを提供する方法についてのガイダンスが提供されています。

Excel VBAのSelectionプロパティを理解し、適切に利用することで、アプリケーションの操作性や効率を大幅に向上させることができます。

————-

Application.Selection property (Excel) | Microsoft Learn

Source link

The Selection property in VBA for Excel retrieves the currently selected object on the active worksheet as part of an Application object. If no object is selected, it returns Nothing. You can use the Select method to change the selection, and the TypeName function to identify the type of selected object.

Syntax

vba
expression.Selection

  • expression: A variable representing an Application object.

Behavior

  • The type of object returned depends on the selection, such as a Range object for selected cells.
  • If nothing is selected, it returns Nothing.
  • Without an object qualifier, it is equivalent to Application.Selection.

Examples

  1. Clear Selection on a Worksheet:
    vba
    Worksheets(“Sheet1”).Activate
    Selection.Clear

  2. Display Object Type:
    vba
    Worksheets(“Sheet1”).Activate
    MsgBox “The selection object type is ” & TypeName(Selection)

  3. Information About Current Selection:
    vba
    Sub TestSelection()
    Dim str As String
    Select Case TypeName(Selection)
    Case “Nothing”
    str = “No selection made.”
    Case “Range”
    str = “You selected the range: ” & Selection.Address
    Case “Picture”
    str = “You selected a picture.”
    Case Else
    str = “You selected a ” & TypeName(Selection) & “.”
    End Select
    MsgBox str
    End Sub

Support

For questions or feedback on Office VBA, refer to the Office VBA support and feedback channels for assistance.

関連記事