Programing

VBA에서 dim과 set의 차이점은 무엇입니까

lottogame 2020. 10. 24. 09:24
반응형

VBA에서 dim과 set의 차이점은 무엇입니까


VBA의 초보자로 용서하십시오.

가끔 사용

Dim r as Range
r = Range("A1")

내가 사용하는 다른 시간

Set r = Range("A1")

차이점은 무엇입니까? 그리고 언제 무엇을 사용해야합니까?


set객체 참조를 참조하지 않는 한 사용할 이유가 없습니다 . 해당 컨텍스트에서만 사용하는 것이 좋습니다. 다른 모든 단순 데이터 유형의 경우 할당 연산자를 사용하십시오. dim그러나 모든 변수 (차원)으로 지정 하는 것이 좋습니다 .

단순 데이터 유형의 예로는 것 integer, long, boolean, string. 이들은 데이터 유형일 뿐이며 자체 메서드와 속성이 없습니다.

Dim i as Integer
i = 5

Dim myWord as String
myWord = "Whatever I want"

의 예는 objectRange하는 Worksheet, 또는를 Workbook. 여기에는 자체 메서드와 속성이 있습니다.

Dim myRange as Range
Set myRange = Sheet1.Range("A1")

없이 마지막 줄을 사용하려고하면 SetVB에서 오류가 발생합니다. 이제 object선언되었으므로 속성 및 메서드에 액세스 할 수 있습니다.

myString = myRange.Value


그러나 나는 이것이 당신이 정말로 요구하는 것이라고 생각하지 않습니다.

때때로 나는 다음을 사용합니다.

    Dim r as Range
    r = Range("A1")

이것은 작동하지 않습니다. Set그렇지 않으면 런타임 오류 # 91 개체 변수 또는 With 블록 변수가 설정되지 않았습니다 . 개체 참조에 변수 값을 할당 하려면을 사용해야 Set하기 때문 입니다. 그러면 위의 코드 작동합니다.

나는 아래 코드가 당신이 정말로 묻는 것을 설명한다고 생각합니다 . 의 우리가 유형을 선언하지 않는 가정하자 rVariant대신 유형입니다.

Public Sub test()
    Dim r
    debug.print TypeName(r)

    Set r = Range("A1")
    debug.print TypeName(r)

    r = Range("A1")
    debug.print TypeName(r)
End Sub

자, 여기서 일어나는 일을 분석해 봅시다.

  1. r Variant로 선언 됨

    `Dim r` ' TypeName(r) returns "Empty", which is the value for an uninitialized variant
    
  2. rRange포함 셀 "A1" 로 설정됩니다.

    Set r = Range("A1") ' TypeName(r) returns "Range"
    
  3. r받는 사람 설정 기본 속성Range("A1").

    r = Range("A1") ' TypeName(r) returns "String"
    

이 경우 Range의 기본 속성은 .Value이므로 다음 두 줄의 코드는 동일합니다.

r = Range("A1")
r = Range("A1").Value

기본 개체 속성에 대한 자세한 내용은 Chip Pearson의 "클래스의 기본 멤버" 를 참조하십시오 .


예를 Set들어 :

내가 사용하는 다른 시간

Set r = Range("A1")

This wouldn't work without first declaring that r is a Range or Variant object... using the Dim statement - unless you don't have Option Explicit enabled, which you should. Always. Otherwise, you're using identifiers that you haven't declared and they are all implicitly declared as Variants.


Dim: you are defining a variable (here: r is a variable of type Range)

Set: you are setting the property (here: set the value of r to Range("A1") - this is not a type, but a value).

You have to use set with objects, if r were a simple type (e.g. int, string), then you would just write:

Dim r As Integer
r=5

Dim simply declares the value and the type.

Set assigns a value to the variable.


If a variable is defined as an object e.g. Dim myfldr As Folder, it is assigned a value by using the keyword, "Set".


Dim is short for Dimension and is used in VBA and VB6 to declare local variables.

Set on the other hand, has nothing to do with variable declarations. The Set keyword is used to assign an object variable to a new object.

Hope that clarifies the difference for you.


According to VBA help on SET statement it sets a reference to an object.so if you change a property the actual object will also changes.

Dim newObj as Object
Set var1=Object1(same type as Object)
Set var2=Object1(same type as Object)
Set var3=Object1(same type as Object)
Set var4=Object1(same type as Object)
Var1.property1=NewPropertyValue

the other Vars properties also changes,so:

Var1.property1=Var2.property1=Var3.property1=Var4.property1=Object1.Property1=NewpropertyValue`

actualy all vars are the same!

참고URL : https://stackoverflow.com/questions/3872339/what-is-the-difference-between-dim-and-set-in-vba

반응형