VBAセットステートメント| オブジェクト変数に値を割り当てる方法は?

Excel VBASetステートメント

VBA Setは、オブジェクトまたは変数への参照を示す値キーを割り当てるために使用されるステートメントです。たとえば、Set M = Aと書くと、この関数を使用して特定の変数のパラメーターを定義します。参照には、Aと同様の同じ値と属性があります。

VBAでは、オブジェクトがないと何もできないため、オブジェクトはExcelのコアです。オブジェクトは、ワークブック、ワークシート、および範囲です。変数を宣言するときは、それにデータ型を割り当てる必要があります。また、オブジェクトをデータ型として割り当てることもできます。宣言されたオブジェクト変数に値を割り当てるには、「SET」という単語を使用する必要があります。「セット」という言葉は、VBAの新しいオブジェクトを指すために使用されます。たとえば、特定のワークシートの特定の範囲を指します。

Excel VBA Setステートメントの使用方法は?

このVBAセットステートメントテンプレートはここからダウンロードできます–VBAセットステートメントテンプレート

#1-範囲オブジェクト変数を使用したステートメントの設定

たとえば、A1からD5の範囲を頻繁に使用するとします。変数をrangeとして宣言し、範囲参照をRange( "A1:D5")として設定できるたびに、コードをRange( "A1:D5")として記述する代わりに

手順1:変数をRangeオブジェクトとして宣言します。

コード:

 サブセットSet_Example()

MyRange AsRangeを暗くする

エンドサブ

ステップ2:データ型を範囲として割り当てる瞬間に、「設定」という単語を使用します。

コード:

 Sub Set_Example()Dim MyRange As Range Set MyRange = End Sub 

ステップ3:範囲について説明します。

コード:

 Sub Set_Example()Dim MyRange As Range Set MyRange = Range( "A1:D5")End Sub 

ステップ4:これで、変数「MyRange」は範囲A1からD5に等しくなります。この変数を使用して、この範囲のすべてのプロパティとメソッドにアクセスできます。

コピーしたり、Excelでコメントを追加したり、その他多くのことができます。

たとえば、ここでいくつかの番号を作成しました。

変数を使用して、フォントサイズを12に変更します。

コード:

 Sub Set_Example()Dim MyRange As Range Set MyRange = Range( "A1:D5")MyRange.Font.Size = 12 End Sub 

これにより、割り当てられた範囲のフォントサイズが変更されます。

このように、「セット」という言葉を使うことで、特定の範囲で多くのことができます。

#2 –ワークシートオブジェクト変数を使用したステートメントの設定

「set」がVBAの範囲オブジェクトでどのように機能するかを見てきました。ワークシートオブジェクトとまったく同じように機能します。

ワークブックに5つのワークシートがあり、特定の1つのワークシートに戻りたい場合は、そのワークシート名を定義済みのオブジェクト変数に設定できます。

たとえば、以下のコードを見てください。

コード:

 Sub Set_Worksheet_Example()Dim Ws As Worksheet Set Ws = Worksheets( "Summary Sheet")End Sub 

In the above code, the variable “Ws” defined as object variable, and in the next line by using the word “Set” we set the variable to the worksheet named “Summary Sheet”.

Now by using this variable, we can do all the things associated with it. Take a look at the below two sets of code.

#1 – Without “Set” Word

Code:

 Sub Set_Worksheet_Example1() 'To select the sheet Worksheets("Summary Sheet").Select 'To Activate the sheet Worksheets("Summary Sheet").Activate 'To hide the sheet Worksheets("Summary Sheet").Visible = xlVeryHidden 'To unhide the sheet Worksheets("Summary Sheet").Visible = xlVisible End Sub 

Every time I have used the worksheets object to refer to the sheet “Summary Sheet”. This makes the code so lengthy and requires a lot of time to type.

As part of the huge code, it is frustrating to type the worksheet name like this every time you need to reference the worksheet.

Now take a look at the advantage of using the word Set in Code.

#2 – With “Set” Word

Code:

 Sub Set_Worksheet_Example() Dim Ws As Worksheet Set Ws = Worksheets("Summary Sheet") 'To select the sheet Ws.Select 'To Activate the sheet Ws.Activate 'To hide the sheet Ws.Visible = xlVeryHidden 'To unhide the sheet Ws.Visible = xlVisible End Sub 

The moment we set the worksheet name we can see the variable name while entering the code as part of the list.

#3 – Set Statement with Workbook Object Variables

The real advantage of the word “Set” in VBA arises when we need to reference different workbooks.

When we work with different workbooks it is so hard to type in the full name of the workbook along with its file extension.

Assume you have two different workbooks named “Sales Summary File 2018.xlsx” and “Sales Summary File 2019.xlsx” we can set the two workbooks like the below code.

Code:

 Sub Set_Workbook_Example1() Dim Wb1 As Workbook Dim Wb2 As Workbook Set Wb1 = Workbooks("Sales Summary File 2018.xlsx") Set Wb2 = Workbooks("Sales Summary File 2019.xlsx") End Sub 

Now variable Wb1 is equal to the workbook named “Sales Summary File 2018.xlsx” and variable Wb2 is equal to the workbook named “Sales Summary File 2019.xlsx”.

Using this variable we can actually access all the properties and methods associated with the workbook.

We can shorten the code like the below.

Without Using Set Keyword to activate the workbook:

Workbooks("Sales Summary File 2018.xlsx").Activate

Using the Set Keyword to activate the workbook:

Wb1.Activate

This makes the writing of the code lot simpler and also once the workbook name is set there is a worry of typo error of the workbook names.