VBA: when to use :=

imaguy77

New Member
Joined
Aug 26, 2008
Messages
21
When do you use the := and what does it mean?

Also, if you have option explicit on and you want to dimension the following

dim cell as ___

for something like

for each cell in selection

what do you dim cell to be? variant? or anything more specific?


Thanks!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I'm not sure what := means, but about the only place I've used it is in a Find command...something like:

Code:
With ActiveSheet.Range("A1:Z100")
   .Find("Hello", Lookin:=xlValues)
    '.....etc, etc
End WIth

But as far as WHY is has to be " := " and not just " = ", I don't know but I'd be interested to hear from someone who does.


Code:
Dim Cell as Range
 
Upvote 0
Oh yea, ok...I suppose I've seen it there too, lol.

I'm sure there are more types of command lines that would require the :=, but even still, I'm not sure what is special about those types of commands. I'm curious as well.
 
Upvote 0
The basic structure of VBA is
Object.Method

When you see the := (colon-equals), you know that you are looking at parameters of how the Object should be performed.
Example:

Ball.Kick Direction:=Left, Force:=Hard, Elevation:=High, etc

HTH
Beth
 
Upvote 0
Hi

Some more comments about the assignment operator ":="

":=" is an assignement operator for parameters. You can use it with object methods but also with for ex. functions. It's optional if you respect the order of the parameters

It's used usually in 2 cases

1 - for documentation or readability of the code

ex.: These 2 statements are equivalent:

Code:
Msgbox "Hi!",vbYesNo,"Greeting"
 
MsgBox Prompt:="Hi!", Buttons:=vbYesNo, Title:="Greeting"

You can argue, however, that the second is more informative or makes the code easier to read and understand since you are seeing the names of the parameters.

2 - To use just some parameters and avoid listing empty parameters

Ex.:

The Application.Inputbox method is defined in the help as:

Code:
expression.InputBox(Prompt, Title, Default, Left, Top, HelpFile, HelpContextID, Type)

If you want to select a range you can use:

Code:
Dim r As Range
Set r = Application.InputBox(Prompt:="Select range", Type:=8)

Just listing the first and last parameters.

You could also use

Code:
Dim r As Range
Set r = Application.InputBox("Select range", , , , , , , 8)

but it's not so easy to read.

Remark:

1 - If you use the := operator you can write the parameters in whatever order you like, ex.:

Code:
MsgBox Title:="Greeting", Buttons:=vbYesNo, Prompt:="Hi!"

2 - You can also use the := with just the parameters that do not follow the default order, like

Code:
Dim r As Range
Set r = Application.InputBox("Select range", Type:=8)
 
Upvote 0
A side note is not to use reserved words like "Cell" as a variable (or "Range", "Target", etc). "c" works fine, and won't cause you confilicting problems with VB when it compiles.

HTH,
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top