-1

I am working on vba and came across :=
What it does actually?

Sample example:
msg:=Var1 & ": " & Chr(34)

Basically my concern is whether it works like in linux, where you can use things like LIBPATH:= many library path or is it just like C language where it is simply used to assign a value to the variable?

Community
  • 1
  • 1
Aakash Goyal
  • 1,051
  • 4
  • 12
  • 44

3 Answers3

2

This is an named argument.

A named argument consists of an argument name followed by a colon and an equal sign (:=), followed by the argument value. See HERE

L42
  • 19,427
  • 11
  • 44
  • 68
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
2

:= is used when you assign value to Arguments.
It can be Arguments of Object Method or Procedure.

Example1:

myFilename = Application.GetOpenFilename(filefilter:="Excel Files (*.xlsx), *.xlsx")

In the code above, you use := to assign value to filefilter argument in GetOpenFilename method.

Example2:

Suppose you have this Procedure.

Private Sub MyProcedure(MyValue as String)
'~~> code here
End Sub

Then you have your Main Sub

Sub Main()

mystring = "Hello"
'~~> assign value to MyProcedure argument
MyProcedure MyValue:=mystring

End Sub

Although, you really don't need to use := in assigning Procedure Argument.
Below also works:

MyProcedure mystring

Hope this helps.

L42
  • 19,427
  • 11
  • 44
  • 68
  • 1
    Just one more clarification. In linux, I think it helps to assign multiple values to a variable (for eg, you can assign many library paths to one variable LIBPATH using this `:=`). So, is it like that in vba also? – Aakash Goyal Jan 24 '14 at 07:36
  • 1
    No, it doesn't work that way in `VBA`. If you want to assign multiple values in a `Variable` you simply declare that `Variable` as `Variant` type then directly pass the multiple values using `=`. – L42 Jan 24 '14 at 07:49
1

In some languages, := 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

But in vba It is used for "named arguments",

Linga
  • 10,379
  • 10
  • 52
  • 104