2
What are the differences between the Dim
and the Set
? When to use each?
Context 1
Dim minhaString as String
minhaString = "Olá!"
Context 2
Set minhaString = "Olá!"
2
What are the differences between the Dim
and the Set
? When to use each?
Context 1
Dim minhaString as String
minhaString = "Olá!"
Context 2
Set minhaString = "Olá!"
6
Dim
and Set
are reserved words of some languages like Visual Basic, Vbscript and VBA (Visual Basic for Applications).
The reserved word Dim
is used to declare a variable. The word Set
is used to assign an object reference to a variable.
Examples in Vbscript:
Dim numero
numero = 2
Dim xlApp
Set xlApp = CreateObject("Excel.Application")
Note that when we are dealing with objects in Vbscript and VBA it is necessary to use the word Set
before the variable. This happens, because we are keeping in the variable the reference of an object. On the other hand, when we are dealing with numbers, string, booleans this is not necessary. Because of this, the variable numero
above received the value 2
without the need of the reserved word Set
.
Therefore, it is not the case to use Dim
or Set
as suggested by the question. These are words reserved for different uses. One to declare (Dim
) another to assign (Set
).
There is another use for the word Set which is in the definition of properties of a class, however, I believe this is outside the scope of the question, since the relationship between Dim and Set has been questioned.
Browser other questions tagged vba variables
You are not signed in. Login or sign up in order to post.
Could you elaborate your question better? In the context of VBA are separate commands. "Set" has to do with variable assignment. " Dim" has to do with variable scope of use.
– Pagotti