VBA 세트 설명 | 개체 변수에 값을 할당하는 방법은 무엇입니까?

Excel VBA Set 문

VBA Set는 개체 또는 변수에 대한 참조를 나타내는 값 키를 할당하는 데 사용되는 입니다. 예를 들어이 함수를 사용하여 특정 변수에 대한 매개 변수를 정의합니다. 예를 들어 Set M = A를 작성하면 이제 M을 의미합니다. 참조는 A가 가진 것과 유사한 동일한 값과 속성을 갖습니다.

VBA에서 개체가 없으면 우리가 아무것도 할 수 없기 때문에 개체가 엑셀의 핵심입니다. 개체는 통합 문서, 워크 시트 및 범위입니다. 변수를 선언 할 때 데이터 유형을 할당해야하며 객체를 데이터 유형으로 할당 할 수도 있습니다. 선언 된 개체 변수에 값을 할당하려면 "SET"이라는 단어를 사용해야합니다. 예를 들어 특정 워크 시트의 특정 범위를 나타내는 VBA의 새 개체를 나타내는 데 사용되는 "세트"라는 단어입니다.

Excel VBA Set 문을 사용하는 방법은 무엇입니까?

이 VBA Set 문 템플릿은 여기에서 다운로드 할 수 있습니다 – VBA Set 문 템플릿

# 1 – 범위 개체 변수가있는 set 문

예를 들어 A1에서 D5까지의 범위를 자주 사용한다고 가정합니다. 코드를 Range ( "A1 : D5")로 작성하는 대신 변수를 범위로 선언하고 범위 참조를 Range ( "A1 : D5")로 설정할 수 있습니다.

1 단계 : 변수를 Range 개체로 선언합니다.

암호:

 하위 Set_Example ()

Dim MyRange As Range

End Sub

2 단계 : 데이터 유형을 범위로 지정하는 순간 "Set"이라는 단어를 사용합니다.

암호:

 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 

이렇게하면 할당 된 범위의 글꼴 크기가 변경됩니다.

이렇게“Set”이라는 단어를 사용하여 특정 범위로 많은 작업을 수행 할 수 있습니다.

# 2 – 워크 시트 개체 변수로 문 설정

VBA에서 범위 개체와 함께 "set"이 작동하는 방식을 살펴 보았습니다. 워크 시트 개체와 동일하게 작동합니다.

통합 문서에 5 개의 워크 시트가 있고 하나의 특정 워크 시트로 계속 돌아가고 싶다면 해당 워크 시트 이름을 정의 된 개체 변수로 설정할 수 있습니다.

예를 들어, 아래 코드를보십시오.

암호:

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

위 코드에서 변수 “Ws” 는 개체 변수로 정의되었고 다음 줄에서는“Set”이라는 단어를 사용하여 변수를“Summary Sheet”라는 워크 시트로 설정합니다.

이제이 변수를 사용하여 관련된 모든 작업을 수행 할 수 있습니다. 아래 두 세트의 코드를 살펴보십시오.

# 1 – "Set"단어없이

암호:

 Sub Set_Worksheet_Example1 () 'To select the sheet Worksheets ( "Summary Sheet").'To Activate the sheet Worksheets ( "Summary Sheet"). Activate 'To hide the sheet Worksheets ( "Summary Sheet"). Visible = xlVeryHidden'To 시트 숨기기 해제 Worksheets ( "Summary Sheet"). Visible = xlVisible End Sub 

워크 시트 개체를 사용하여 "요약 시트"시트를 참조 할 때마다. 이로 인해 코드가 너무 길어지고 입력하는 데 많은 시간이 걸립니다.

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.