# Office VBA usage of :=



## qucho (Jan 10, 2006)

Can someone give an explanation of the usage of ':=' (colon equal) ?

I see it often in sample code within the help of Office VBA, but I have never truly understood the when and why.


----------



## hverne (Mar 19, 2005)

qucho said:


> Can someone give an explanation of the usage of ':=' (colon equal) ?
> 
> I see it often in sample code within the help of Office VBA, but I have never truly understood the when and why.


:= is called an assignment statement. The contents on the right side of the ":=" are stored into the variable on the left side, e.g _sum:=5+2_

One could also use the = sign on a logical equation, e.g _if age = 65 ..._.

Some languages like to keep the 2 uses for the "=" separate, by using := for the assignment.


----------



## Aftab (Oct 15, 2003)

In some languages this operator is used for assignment operations ( x := 5, meaning store 5 in x), but in VBA/VB this operator has a different purpose. It is used for "named arguments", where you can pass parameter values to a proc in any order you like but you have to say which parameter you are suppying the value for.

eg.

Public Sub Showname(surname as string , firstname as string)
MsgBox "Hello " + firstname + " " + surname
end sub

if you called the above procedure like this:
showname "homer" , "simpson"

you will see "Hello Simpson Homer"

but is you call it like this:
showname firstname := "homer" , surname := "simpson"

you will get the desired result.

In VBA/VB the assignment operator is the equals sign (=).

Hope this helps.


----------



## cristobal03 (Aug 5, 2005)

Thought I'd mention this because it's suggested in the title of post #2.

VBA/VB does not have an *equality operator* like many other languages--or rather, VBA's equality operator and assignment operator are the same character. This is a bit of a technical minutia as far as VBA/VB is concerned, since the language is *non-strict*.

In many languages, once a variable has been dimensioned--or created--it is then assigned a value using a specific operator (in this case, *=*), like so:


```
Dim varSomeVariable As Variant

varSomeVariable = "Hello world!"
```
Then, in order to check the value contained in the variable, one would use a characteristically different operator to test the _equality_ of two expressions. For most languages I know, the equality operator is *==*, so some sample code might look like this:


```
If varSomeVariable == "Hello world!" Then
  ' the value stored in varSomeVariable is the same as "Hello world!"
Else
  ' some other value is stored in varSomeVariable
End If
```
In other words, using the above logic, *variable = value* means, "Place *value* in container *variable* for future use," whereas *variable == value* means, "Discover if *value* is the same as whatever is contained in *variable*."

VBA/VB does not support this mechanism. Both of the above expressions would be written *variable = value*. There's a deep pool of further technical minutiae of which this is just a drop, but it is enough to understand that VBA/VB is smart enough (or stupid enough ) to process the following code safely:


```
Dim varSomeVariable As Variant

varSomeVariable = "Hello world!"

If varSomeVariable = "This string is not 'Hello world!'" Then
  ' no worries; varSomeVariable will [i]not[/i] be reassigned
End If
```
HTH

chris.


----------



## qucho (Jan 10, 2006)

Thank you for your responses guys.

I also found that while VBA does not support the ':=' assigment it is in ocation used inside some objects, and this is because this objects are outside of VBA (I.E. connections to databases where you need to declare/assign a value to a variable when createing such object, and other COM objects).

Very nice replies you guys gave =)


----------

