What is the difference between dim and set in vba

2019-01-02 20:34发布

Pardon me as am a newbie in VBA.

Sometimes I use

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

Other times I use

Set r = Range("A1")

What is the difference? And when should I use what?

7条回答
临风纵饮
2楼-- · 2019-01-02 21:12

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
查看更多
登录 后发表回答